存储过程代码如下:
- ALTER PROCEDURE dbo.testOutput
- (
- @p1 int ,
- @p2 int OUTPUT,
- @p3 int
- )
- AS
- /* SET NOCOUNT ON */
- select @p2 = count(*) from testProc where testid between @p1 and @p3
- RETURN @@rowcount
这个存储过程返回2个值,一个是output型参数@p2,另外一个是数据库自带的return值 @@rowcount(语句所影响的行数)。
C#程序:
- SqlCommand com = new SqlCommand( "testOutput" ,con);
- com.CommandType = CommandType.StoredProcedure;
- SqlParameter p1 = new SqlParameter( "@p1" ,SqlDbType.Int);
- SqlParameter p2 = new SqlParameter( "@p2" ,SqlDbType.Int);
- SqlParameter p3 = new SqlParameter( "@p3" ,SqlDbType.Int);
- SqlParameter rowcount = new SqlParameter( "@@rowcount" , SqlDbType.Int);
- p1.Value = int .Parse(textBox2.Text);
- p2.Direction = ParameterDirection.Output; //把p2设置为output型参数
- p3.Value = int .Parse(textBox3.Text);
- rowcount.Direction = ParameterDirection.ReturnValue; //把rowcount类////型设置为returnvalue型
- com.Parameters.Add(p1);
- com.Parameters.Add(p2);
- com.Parameters.Add(p3);
- com.Parameters.Add(rowcount);
- com.ExecuteNonQuery();
- MessageBox.Show(p2.Value.ToString()); //执行过存储过程以后,output参数p2和@@rowcount就自动返回了值。
- MessageBox.Show(rowcount.Value.ToString());
本文介绍了一个SQL Server存储过程的创建及其通过C#程序进行调用的方法。该存储过程用于计算指定范围内的记录数量,并返回一个输出参数及受影响的行数。此外,还展示了如何在C#中配置存储过程参数类型。
4507

被折叠的 条评论
为什么被折叠?



