Home > Database > Mysql Tutorial > 存储过程的输出参数,返回值与结果集

存储过程的输出参数,返回值与结果集

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 18:00:56
Original
1295 people have browsed it

存储过程中可以定义输出变量,返回值,执行存储过程还能获得结果集。

每个存储过程都有默认的返回值,默认值为0。下面我们分别看看在management studio中如何查看输出参数,返回值以及结果集,然后我们再在ASP.NET调用存储过程中如何获得输出参数,返回值以及结果集。

首先:在sql server management studio中查看输出参数,返回值以及结果集。本示例以Northwind数据库为例。
代码如下:
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

输出的结果如下:

可以看出是通过EXEC @return_value = [dbo].[Employee] @Rowcount = @MyOutput OUTPUT中的return_value变量来获得返回值,而用MyOutput变量来获得了输出变量值,此处的MyOutput变量相当于引用传递!
接下来将讨论ASP.NET调用存储过程中如何获得输出参数,返回值以及结果集。

代码如下:
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()+"
");
Response.Write(cmd.Parameters["@return_value"].Value.ToString());
}

此处定义了Rowcount输出变量,以及return_value返回值变量。输出Roucount输出变量的值为9,而return_value的值为0。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template