您的位置:首页 > 其它

调用存储过程的一些方法

2013-04-08 19:04 417 查看
protected void Button1_Click(object sender, EventArgs e)
   {
    
       int number =Convert.ToInt32(TextBox1.Text.ToString().Trim());
       int tid =Convert.ToInt32(TextBox2.Text.ToString().Trim());
       int k = exec_proc_2(number, tid);
       bind();
       Response.Write(k.ToString());
    
     
   }
   protected void bind()
   {
       SqlConnection conn = new SqlConnection("server=(local);database=yiqi;uid=monkey;pwd=donkey");
       conn.Open();
       SqlCommand cmd = new SqlCommand("proc_3", conn);
       cmd.CommandType = CommandType.StoredProcedure;
       DataTable dt = new DataTable();
       using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
       {
           dt.Load(sdr);
       }
       GridView1.DataSource = dt;
       GridView1.DataBind();

   }

   public static int exec_proc_2(int number, int tid)
   {
       SqlConnection conn = new SqlConnection("server=(local);database=yiqi;uid=monkey;pwd=donkey");
       conn.Open();
       SqlCommand cmd = new SqlCommand("proc_2", conn);//同上
       cmd.CommandType = CommandType.StoredProcedure;//同上
       cmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int));//添加一个名为@result的参数,数据类型为SqlDbType.Int
       cmd.Parameters["@result"].Direction = ParameterDirection.Output;//将@result参数设置成为接收输出参数
       cmd.Parameters.AddWithValue("@number", number);
       cmd.Parameters.AddWithValue("@tid", tid);
       cmd.ExecuteNonQuery();
       int result = (int)cmd.Parameters["@result"].Value;//将输出的Object数据转换成int类型
       return result;
   }

 

 

-----------------------------------------------------------------------------------------------------

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROC [dbo].[proc_1]

AS

Insert t1(number) Values(200)

 

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROC [dbo].[proc_2]

@result int output,

@number int,

@tid int

AS

Update t1 Set number=number+@number Where tid=@tid

Select @result=number From t1

 

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROC [dbo].[proc_3]
AS
select * from t1

 

--------------------------------------

Create Table Table_1

(

tid int identity(1,1),

number int

)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: