存储过程中可以定义输出变量,返回值,执行存储过程还能获得结果集。 每个存储过程都有默认的返回值,默认值为0。下面我们分别看看在management studio中如何查看输出参数,返回值以及结果集,然后我们再在ASP.NET调用存储过程中如何获得输出参数,返回值以及结果集。 create proc Employee @Rowcount int=0 output as begin SELECT * FROM [Northwind].[dbo].[Employees] set @Rowcount=@@ROWCOUNT end
USE [Northwind] GO DECLARE @return_value int, @MyOutput int EXEC @return_value = [dbo].[Employee] @Rowcount = @MyOutput OUTPUT SELECT @MyOutput as N'输出参数的值' SELECT 'Return返回的值' = @return_value GO
protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=1;"); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText="Employee"; cmd.CommandType = CommandType.StoredProcedure; SqlParameter para = new SqlParameter("@Rowcount", SqlDbType.Int, 4); para.Direction = ParameterDirection.Output; cmd.Parameters.Add(para); cmd.Parameters.Add("@return_value", SqlDbType.Int, 4); cmd.Parameters["@return_value"].Direction = ParameterDirection.ReturnValue; con.Open(); cmd.ExecuteNonQuery(); Response.Write(cmd.Parameters["@Rowcount"].Value.ToString()+"<br/>"); Response.Write(cmd.Parameters["@return_value"].Value.ToString()); }
|