日期:2014-05-20  浏览次数:20727 次

调用存储过程
请问在C#.net中怎样调用下面的存储过程来获取返回值,:

以下是存储过程:
Create   Proc   P_Test  
(   @ny   varchar(20),
  @ny1   varchar(20),
@Result   int   OUTPUT)
As
SET   NOCOUNT   ON       --关闭回响
declare   @jine   varchar(50)
declare   @shuliang   varchar(50)
declare   @sum_hzmx     int
declare   @sum_tb_huikuaninfo     int
declare   @val_tb_huikuaninfo     int
set   @val_tb_huikuaninfo=0
set   @sum_hzmx=0
set   @sum_tb_huikuaninfo=0
Declare   HR_cursor   cursor   for   select   jine,shuliang     from   tb_hzmx   where   dz_date=@ny   order   by   jine
open   HR_cursor
Fetch   Next   from   HR_cursor   into   @jine,@shuliang
while   @@Fetch_status=0
begin
set   @sum_hzmx=@sum_hzmx+CAST(@shuliang   as     int)
select   @val_tb_huikuaninfo=count(*)     from   tb_huikuaninfo   where   dzdate=@ny1   and   HueiKuanMoney=@jine
set       @sum_tb_huikuaninfo=@sum_tb_huikuaninfo+CAST(@val_tb_huikuaninfo   AS   int)
--select   @sum_tb_huikuaninfo
Fetch   Next   From   HR_cursor   into   @jine,@shuliang
end
close   HR_cursor
Deallocate   HR_cursor
--select   @sum_hzmx
--select   @sum_tb_huikuaninfo
--如果记录数据相同返回true  
if   @sum_hzmx=@sum_tb_huikuaninfo
begin
select   @Result=     1
return   @Result
end
else
--如果记录数据不相同返回false  
begin
select   @Result=     0
return     @Result
end
SET   NOCOUNT   OFF

执行:
declare   @Result   int
exec   P_Test   '2007-11-15 ', '20071115 ',@Result   OUTPUT
select   @Result

------解决方案--------------------
string ny = String.Empty;
string ny2 = String.Empty;
int result = 0;

using (SqlConnection myConnection = new SqlConnection( "... "))
{
SqlCommand myCommand = new SqlCommand( "P_Test ", myConnection);

myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue( "@ny ", ny);
myCommand.Parameters.AddWithValue( "@ny1 ", ny1);

myCommand.Parameters.Add( "@Result ", SqlDbType.Int);
myCommand.Parameters[ "@Result "].Direction = ParameterDirection.Output;

myConnection.Open();
myCommand.ExecuteNonQuery();

result = (int)myCommand.Parameters[ "@Result "];
}

result 为所求