try something like the following, note, you have to close Reader first
cmd = new SqlCommand("aaaa",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ret", SqlDbType.Int);
cmd.Parameters["@ret"].Direction = ParameterDirection.Output;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//
}
reader.Close();
int ret = (int)cmd.Parameters["@ret"].Value;
---------------------------------------------------------------------------------------------
1.获取Return返回值
程序代码//存储过程
//Create PROCEDURE MYSQL
// @a int,
// @b int
//AS
// return @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 10;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
MyCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@return"].Value.ToString());
2.获取Output输出参数值
程序代码
//存储过程
//Create PROCEDURE MYSQL
// @a int,
// @b int,
// @c int output
//AS
// Set @c = @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@c", SqlDbType.Int));
MyCommand.Parameters["@c"].Direction = ParameterDirection.Output;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@c"].Value.ToString());
----------------------存储过程共有三种返回值-------------------
/*(一)、参数返回值@parm2;
(二)、过程返回值return 100;
(三)、数据集返回值select * from table1 where age = @parm1
请问,如何得到三类返回值?
*/
create table table1(
name varchar(30),
age integer,
primary key(name, age)
)
create procedure proc1
@parm1 integer,
@parm2 varchar(30) output
as
select @parm2 = 'zhongguo'
select * from table1 where age = @parm1
return 100
--(一)、参数返回值@parm2;
declare @re varchar(30)
exec proc1 1,@re out
select 返回结果=@re
--(二)、过程返回值return 100;
declare @re int,@parm2 varchar(30)
exec @re=proc1 1,@parm2 out
select 返回结果=@re
--(三)、数据集返回值select * from table1 where age = @parm1
--定义返回结果保存的临时表(因为是返回记录集,所以要用临时表)
--调用返回结果
--declare @re varchar(30)
--insert #re exec proc1 1,@re out
select * into #re from OPENROWSET('SQLOLEDB',
'SERVER=zhou;uid=sa;pwd=zhou;Database=pubs','SET FMTONLY OFF;SET NOCOUNT ON declare @re varchar(30) exec proc1 ''1'',@re out') as a
--显示结果
select * from #re
--处理完成后删除临时表
drop table #re
--返回数据集方式一:
/*补充*/
execute sp_executesql
N'select * from pubs.dbo.employee where job_lvl = @level',
N'@level tinyint',
@level = 35
或者多条件
execute sp_executesql
N'select * from IndividualSaleHouse where user_ID = @user_ID and Area_ID=@Area_ID ',
N'@user_ID int,@Area_ID int',
@user_ID = 1,@Area_ID=1
--返回单个值:
/* 补充 */
declare @sql nvarchar(200)
declare @cnt int
set @sql=N'select @cnt=count(*) from WQ_STINFO_B '
exec sp_executesql @sql,N'@cnt int output',@cnt output