欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#获取存储过程返回值和输出参数值的方法

程序员文章站 2024-02-16 23:27:34
1.获取return返回值 复制代码 代码如下://存储过程//create procedure mysql//     @a i...

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());

c#接收存储过程返回值:

复制代码 代码如下:

     public static int user_add(user us)
     {
         int iret;
         sqlconnection conn = new sqlconnection(conn_str);
         sqlcommand cmd = new sqlcommand("user_add", conn);
         cmd.commandtype = commandtype.storedprocedure;
         cmd.parameters.addwithvalue("@uname", us.uname);
         cmd.parameters.addwithvalue("@upass", us.upass);
         cmd.parameters.addwithvalue("@passquestion", us.passquestion);
         cmd.parameters.addwithvalue("@passkey", us.passkey);
         cmd.parameters.addwithvalue("@email", us.email);
         cmd.parameters.addwithvalue("@rname", us.rname);
         cmd.parameters.addwithvalue("@area", us.area);
         cmd.parameters.addwithvalue("@address", us.address);
         cmd.parameters.addwithvalue("@zipcodes", us.zipcodes);
         cmd.parameters.addwithvalue("@phone", us.phone);
         cmd.parameters.addwithvalue("@qq", us.qq);
         cmd.parameters.add("@return_value", "").direction = parameterdirection.returnvalue;      
         try
         {
             conn.open();
             cmd.executenonquery();
             iret = (int)cmd.parameters["@return_value"].value;
         }
         catch (sqlexception ex)
         {
             throw ex;
         }
         finally
         {
             conn.close();
         }
         return iret;
     }

c#接收存储过程输出参数:

复制代码 代码如下:

    public static decimal cart_useramount(int uid)
    {
        decimal iret;
        sqlconnection conn = new sqlconnection(conn_str);
        sqlcommand cmd = new sqlcommand("cart_useramount", conn);
        cmd.commandtype = commandtype.storedprocedure;
        cmd.parameters.addwithvalue("@uid", uid);
        cmd.parameters.add("@amount", sqldbtype.decimal).direction=parameterdirection.output;
        try
        {
            conn.open();
            cmd.executenonquery();
            iret = (decimal)cmd.parameters["@amount"].value;
        }
        catch (sqlexception ex)
        {
            throw ex;
        }
        finally
        {
            conn.close();
        }
        return iret;
    }