存储过程的使用

来源:互联网 发布:网络暴力与道德议论文 编辑:程序博客网 时间:2024/05/17 18:19

存储过程代码如下:

 

  1. ALTER PROCEDURE dbo.testOutput  
  2.     (
  3.     @p1 int ,
  4.     @p2 int OUTPUT,
  5.     @p3 int 
  6.     )   
  7. AS
  8.     /* SET NOCOUNT ON */
  9.     select @p2 = count(*) from testProc where testid between @p1 and @p3
  10.     RETURN @@rowcount

这个存储过程返回2个值,一个是output型参数@p2,另外一个是数据库自带的return值 @@rowcount(语句所影响的行数)。

 

C#程序:

 

  1.             SqlCommand com = new SqlCommand("testOutput",con);
  2.             com.CommandType = CommandType.StoredProcedure;
  3.             SqlParameter p1 = new SqlParameter("@p1",SqlDbType.Int);
  4.             SqlParameter p2 = new SqlParameter("@p2",SqlDbType.Int);
  5.             SqlParameter p3 = new SqlParameter("@p3",SqlDbType.Int);
  6.             SqlParameter rowcount = new SqlParameter("@@rowcount", SqlDbType.Int);   
  7.             p1.Value = int.Parse(textBox2.Text);
  8.             p2.Direction = ParameterDirection.Output; //把p2设置为output型参数
  9.             p3.Value = int.Parse(textBox3.Text);
  10.             rowcount.Direction = ParameterDirection.ReturnValue;//把rowcount类////型设置为returnvalue型
  11.             com.Parameters.Add(p1);
  12.             com.Parameters.Add(p2); 
  13.             com.Parameters.Add(p3);
  14.             com.Parameters.Add(rowcount);
  15.             com.ExecuteNonQuery();
  16.             MessageBox.Show(p2.Value.ToString());  //执行过存储过程以后,output参数p2和@@rowcount就自动返回了值。
  17.             MessageBox.Show(rowcount.Value.ToString());
原创粉丝点击