日期:2014-05-19  浏览次数:20842 次

C# 怎么调用存储过程中返回的变量值 ??在线等!!! 急哦。
我想在程序中调用   @flag   的返回值,   该怎么调用?     散分!!!!   帮忙。。。

CREATE   PROCEDURE     Select_Insert_Cart_Proc
@CID   int,
@PID   int
    AS
declare   @flag   int

set   @flag=0

if   exists(select   *   from   tosell   where   CID=@CID   and   PID=@PID)
begin  
set   @flag=0
end
else
begin

if     not   exists(   select   *   from   cart   where   CID=@CID   and   PID=@PID)
begin  
set   @flag=1
insert   into   cart   (CID,PID)   values   (@CID,@PID)
end  
else
begin  
set   @flag=-1
end
end
return   @flag
GO


------解决方案--------------------
System.Data.SqlClient.SqlConnection scon = new System.Data.SqlClient.SqlConnection( "server=Netangel;uid=etopsUS;pwd=etops;database=etops ");

System.Data.SqlClient.SqlCommand scom = new System.Data.SqlClient.SqlCommand( " ",scon);

scon.Open();

scom.CommandText= "Select_Insert_Cart_Proc ";

scom.CommandType = System.Data.CommandType.StoredProcedure;

//这里再传两个你存储过程用的参数就可以了
scom.Parameters.Add(new SqlParameter( "@flag ", SqlDbType.Int, 4,ParameterDirection.ReturnValue, false, 0, 0,

string.Empty, DataRowVersion.Default, null)) //增加存储过程的返回值参数

scom.ExecuteNonQuery();

Response.Write(scom.Parameters[ "@flag "].Value.ToString()); //输出存储过程的返回值
------解决方案--------------------
在sqlcommand中添加一个参
sqlparameter.add( "@flag ",Sqldbtype.int)
sqlparameter.direction=ParameterDirection.return;
sqlcommand.patameter.add(sqlparameter);
sqlcommand.commandtext= "Select_Insert_Cart_Proc ";
sqlcommand.commandtype=commandtype.StoredProcedure;
sqlcommand.ExecuteNonQuery();
------解决方案--------------------
SqlCommand cmd = new SqlCommand( "XXXXX ",conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter par1 = new SqlParameter( "@flag ", SqlDbType.Int);
cmd.Parameters.Add(par1);
par1.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
int a = int.Parse(par1.Value);
------解决方案--------------------
( "server=Netangel;uid=etopsUS;pwd=etops;database=etops ");
这个改成你自己的就可以了

这个代码我没有调试 ,但是貌似好用

------解决方案--------------------
SqlCommand com1 = new SqlCommand( "Select_Insert_Cart_Proc ",con1);
if (com1.Connection.State == ConnectionState.Closed)
com1.Connection.Open();
com1.CommandType = CommandType.StoredProcedure;
com1.Parameters.Add(new SqlParameter( "@CID ", SqlDbType.Int));
com1.Parameters.Add(new SqlParameter( "@CID ", SqlDbType.Int));
com1.Parameters.Add(new SqlParameter( "@return ", SqlDbType.Int));
com1.Parameters[ "@return "].Direction = ParameterDirection.ReturnValue;
com1.Parameters[ "@AreaNo "].Value = t_NO.Text;
com1.Parameters[ "@AreaName