存储过程代码如下:
- 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());