• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
泛舟
欲穷千里目,更上一层楼
博客园    首页    新随笔    联系   管理    订阅  订阅

ADO.NET 调用存储过程示例

1.创建表:
create table testtb (   idd int not null,
   content varchar(50)
)

2.创建存储过程
create PROCEDURE myprocedure
 @count int,
 @Isok varchar(50) OUTPUT

AS
declare @maxindex int
    select @maxindex=count(*) from testtb ;
 while @count>0
 begin
 insert into testtb (idd,content) values(@count+@maxindex,'content'+convert(varchar(10),@count+@maxindex));
 
 set @count=@count-1; 
  end
 
  set @Isok='ok';
 
 RETURN

3.创建简单 Windows form  实现调用存储过程:
界面代码:

namespace ConsoleApplication1
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(83, 110);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(173, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "执行存储过程,完成插入数据";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(128, 160);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(89, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "是否完成插入:";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(128, 198);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(17, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "NO";
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(116, 69);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 3;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(114, 43);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(113, 12);
            this.label3.TabIndex = 4;
            this.label3.Text = "为表添加的数据量:";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(397, 287);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        #endregion
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label3;
    }
}

--------------------
namespace ConsoleApplication1
{
    
partial class Form1
    {
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>
        private System.ComponentModel.IContainer components = null;

        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>
        
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            
if (disposing && (components != null))
            {
                components.Dispose();
            }
            
base.Dispose(disposing);
        }

        
#region Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 设计器支持所需的方法 - 不要
        
/// 使用代码编辑器修改此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {
            
this.button1 = new System.Windows.Forms.Button();
            
this.label1 = new System.Windows.Forms.Label();
            
this.label2 = new System.Windows.Forms.Label();
            
this.textBox1 = new System.Windows.Forms.TextBox();
            
this.label3 = new System.Windows.Forms.Label();
            
this.SuspendLayout();
            
// 
            
// button1
            
// 
            this.button1.Location = new System.Drawing.Point(83, 110);
            
this.button1.Name = "button1";
            
this.button1.Size = new System.Drawing.Size(173, 23);
            
this.button1.TabIndex = 0;
            
this.button1.Text = "执行存储过程,完成插入数据";
            
this.button1.UseVisualStyleBackColor = true;
            
this.button1.Click += new System.EventHandler(this.button1_Click);
            
// 
            
// label1
            
// 
            this.label1.AutoSize = true;
            
this.label1.Location = new System.Drawing.Point(128, 160);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(89, 12);
            
this.label1.TabIndex = 1;
            
this.label1.Text = "是否完成插入:";
            
// 
            
// label2
            
// 
            this.label2.AutoSize = true;
            
this.label2.Location = new System.Drawing.Point(128, 198);
            
this.label2.Name = "label2";
            
this.label2.Size = new System.Drawing.Size(17, 12);
            
this.label2.TabIndex = 2;
            
this.label2.Text = "NO";
            
// 
            
// textBox1
            
// 
            this.textBox1.Location = new System.Drawing.Point(116, 69);
            
this.textBox1.Name = "textBox1";
            
this.textBox1.Size = new System.Drawing.Size(100, 21);
            
this.textBox1.TabIndex = 3;
            
// 
            
// label3
            
// 
            this.label3.AutoSize = true;
            
this.label3.Location = new System.Drawing.Point(114, 43);
            
this.label3.Name = "label3";
            
this.label3.Size = new System.Drawing.Size(113, 12);
            
this.label3.TabIndex = 4;
            
this.label3.Text = "为表添加的数据量:";
            
// 
            
// Form1
            
// 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            
this.ClientSize = new System.Drawing.Size(397, 287);
            
this.Controls.Add(this.label3);
            
this.Controls.Add(this.textBox1);
            
this.Controls.Add(this.label2);
            
this.Controls.Add(this.label1);
            
this.Controls.Add(this.button1);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.ResumeLayout(false);
            
this.PerformLayout();

        }

        
#endregion

        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.TextBox textBox1;
        
private System.Windows.Forms.Label label3;

    }
}




处理代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String constr = "Data Source=KMGOAL;Initial Catalog=Northwind;Integrated Security=True";

           //关键代码:
             SqlConnection con = new SqlConnection(constr);
            SqlCommand scd = new SqlCommand("myprocedure", con);
            scd.CommandType =CommandType.StoredProcedure;

            SqlParameter parameter1 = scd.Parameters.Add("@count", SqlDbType.Int );
            parameter1.Value =int.Parse(this .textBox1 .Text);           // scd.Parameters["@create_count"] =10;

            SqlParameter parameter2 = scd.Parameters.Add( "@Isok", SqlDbType.VarChar ,10 );
       
     parameter2.Direction = ParameterDirection.Output;
            con.Open();
            scd.ExecuteNonQuery( );
            con.Close();

        this.label2.Text = scd.Parameters["@Isok"].Value.ToString();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Click+= new EventHandler(button1_Click);
        }


       
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());


        }
}

posted @ 2008-08-23 05:02  7goal  阅读(1538)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3