SqlServer获取存储过程返回值的实例

1.ouput参数返回值

复制代码 代码如下:

create procedure [dbo].[nb_order_insert](

@o_buyerid int ,

@o_id bigint output

)

as

begin

set nocount on;

begin

insert into [order](o_buyerid )

values (@o_buyerid )

set @o_id = @@identity

end

end

存储过程中获得方法:

复制代码 代码如下:

declare @o_buyerid int

declare @o_id bigint

exec [nb_order_insert] @o_buyerid,@o_id output

2.return过程返回值

复制代码 代码如下:

create procedure [dbo].[nb_order_insert](

@o_buyerid int ,

@o_id bigint output

)

as

begin

set nocount on;

if(exists(select * from [shop] where [s_id] = @o_buyerid ))

begin

 insert into [order](o_buyerid ) values (@o_buyerid )

 set @o_id = @@identity

 return 1 — 插入成功返回1

end

else

 return 0 — 插入失败返回0 end

存储过程中的获取方法

复制代码 代码如下:

declare @o_buyerid int

declare @o_id bigint

declare @result bit

exec @result = [nb_order_insert] @o_buyerid ,o_id output

3.select 数据集返回值

复制代码 代码如下:

create procedure [dbo].[nb_order_select](

@o_id int

)

as

begin

set nocount on;

select o_id,o_buyerid from [order]

where o_id = @o_id

go

存储过程中的获取方法

(1)、使用临时表的方法

复制代码 代码如下:

create table [dbo].[temp](

[o_id] [bigint] identity(1,1) not for replication not null,

[o_buyerid] [int] not null

)

insert [temp] exec [nb_order_select] @o_id

– 这时 temp 就是exec执行select 后的结果集

select * from [temp]

drop [temp] — 删除临时表

1.获取return返回值

复制代码 代码如下:

sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings[“connstr”].tostring());

conn.open();

sqlcommand mycommand = new sqlcommand(“nb_order”, 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()); //取得return的返回值

2.获取output输出参数值

复制代码 代码如下:

sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings[“connstr”].tostring());

conn.open();

sqlcommand mycommand = new sqlcommand(“nb_order”, 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;   //指定存储过程  addwithvalue可以指定名称和值,而add需要指定名称,类型,再给value

         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;    //取得return的值

         }

         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;  //利用add方法为其添加名称,类型和输出参数

        try

        {

            conn.open();

            cmd.executenonquery();

            iret = (decimal)cmd.parameters[“@amount”].value;  //取得存储过程中的输出参数

        }

        catch (sqlexception ex)

        {

            throw ex;

        }

        finally

        {

            conn.close();

        }

        return iret;

    }

c#取得结果集:

复制代码 代码如下:

string sqlw = string.format(“exec sp_userinfo {0}”, uid);

            datatable dsuser = sqlconn.getdataset(sqlw).tables[0];

 public static dataset getdataset(string sql)
        {
            string connstr = system.configuration.configurationmanager.connectionstrings[“connstr”].tostring();
            sqlconnection conn = new sqlconnection(connstr);
            sqlcommand cmd = new sqlcommand(sql, conn);
            sqldataadapter da = new sqldataadapter(cmd);  //直接用sqldataadapter将结果集取出来放入dataset中
            dataset ds = new dataset();
            da.fill(ds);
            conn.close();
            cmd.dispose();
            return ds;
        }

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐