如何使用存储过程output参数

存储过程代码如下:

 

  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());

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值