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

.Net 调用存储过程取到return的返回值

程序员文章站 2024-02-25 10:49:22
1. 存储过程 set ansi_nulls on go set quoted_identifier on go -- ===========...

1. 存储过程

set ansi_nulls on 
go 
set quoted_identifier on 
go 
-- ============================================= 
-- author: <author,,name> 
-- create date: <create date,,> 
-- description: <description,,> 
-- ============================================= 
alter procedure getorderline 
@orderid varchar(50) 
as 
begin 
-- set nocount on added to prevent extra result sets from 
-- interfering with select statements. 
set nocount on; 

select * from orderline where orderid = @orderid; 

return 123; 
end 
go

 注意 存储过程只能返回 int 类型,如果返回一个字符串 ,将会报类型转化错误

2 后台调用

datatable dt = new datatable(); 
string connstr = system.configuration.configurationmanager.connectionstrings["bll.properties.settings.shoppingdbconnectionstring"].tostring(); 
using(sqlconnection conn= new sqlconnection(connstr)){ 
string callname = "getorderline"; 
using (sqlcommand command = new sqlcommand(callname, conn)) 
{ 
command.commandtype = commandtype.storedprocedure; 
sqlparameter[] sps = { new sqlparameter("@orderid",sqldbtype.varchar,50) , 
new sqlparameter("@return",sqldbtype.int) //注册返回值类型 
}; 

sps[0].value = "43c7cf15-6b2f-4d18-92b2-dbe827f30dfc"; 
sps[1].direction = parameterdirection.returnvalue; //返回参数类型 

command.parameters.addrange(sps); 
using(sqldataadapter sda =new sqldataadapter()){ 
sda.selectcommand = command; 
sda.fill(dt); 
//console.writeline(sda.getfillparameters()[1].value); 
console.writeline(sps[1].value); //取到返回的值 
} 

} 
} 

if(dt.rows.count>0){ 
for (int i = 0; i < dt.rows.count;i++ ) 
{ 
console.writeline(dt.rows[i]["productid"]+":"+dt.rows[i]["productprice"]+":"+dt.rows[i]["productcount"]); 
} 
} 
console.readline();