本文实例讲述了c#调用存储过程的方法。分享给大家供大家参考,具体如下:
create procedure [dbo].[getnamebyid]
@studentid varchar(8),
@studentname nvarchar(50) output
as
begin
select @studentname=studentname from student
where studentid=@studentid
if @@error<>0
return -1
else
return 0
end
using (sqlconnection conn = new sqlconnection(connstr))
{
try
{
sqlcommand cmd = new sqlcommand("getnamebyid", conn);
cmd.commandtype = commandtype.storedprocedure;
cmd.parameters.addwithvalue("@studentid", "09888888"); //给输入参数赋值
sqlparameter paroutput =cmd.parameters.add("@studentname", sqldbtype.nvarchar, 50); //定义输出参数
paroutput.direction = parameterdirection.output; //参数类型为output
sqlparameter parreturn = new sqlparameter("@return", sqldbtype.int);
parreturn.direction = parameterdirection.returnvalue; //参数类型为returnvalue
cmd.parameters.add(parreturn);
conn.open();
cmd.executenonquery();
messagebox.show(paroutput.value.tostring()); //显示输出参数的值
messagebox.show(parreturn.value.tostring()); //显示返回值
}
catch (system.exception ex)
{
messagebox.show(ex.message);
}
}
create procedure addordertran
@country nvarchar(100),
@adds nvarchar(100),
@ynames nvarchar(100),
@pids nvarchar(100),
@cellp nvarchar(100),
@cphone nvarchar(100),
@amounts nvarchar(100),
@cartnumber nvarchar(100)
as
declare @id int
begin transaction
insert into orders(order_country,order_adress,order_username,order_postid,cells,order_phone,total_pay,cartnumber,ispay)
values (@country,@adds,@ynames,@pids,@cellp,@cphone,@amounts,@cartnumber,'0')
select @id=@@identity
insert into orders_item (ordernumber,productsid,products_color,products_price,order_qty,item_total)
select @id,carts_item.productsid,carts_item.products_color,carts_item.products_price,carts_item.item_qty,carts_item.total_pay
from carts_item where carts_item.cartnumber=@cartnumber
delete carts_item where cartnumber=@cartnumber
if @@error <> 0 --发生错误
begin
rollback transaction
return 0
end
else
begin
commit transaction
return @id --执行成功
end
#region 执行存储过程
sqlparameter[] param = new sqlparameter[]
{
new sqlparameter("@country",country),
new sqlparameter("@adds",adds),
new sqlparameter("@ynames",ynames),
new sqlparameter("@pids", pids),
new sqlparameter("@cellp",cellp),
new sqlparameter("@cphone", cphone),
new sqlparameter("@amounts",amounts),
new sqlparameter("@cartnumber",cartnumber),
new sqlparameter("@return",sqldbtype.int)
};
param[8].direction = parameterdirection.returnvalue;
mscl.sqlhelper.runprocedure("addordertran", param);
object obj = param[8].value; //接受返回值
//string connstr = system.configuration.configurationmanager.appsettings["constr"].tostring();
//using (sqlconnection conn = new sqlconnection(connstr))
//{
// conn.open();
// sqlcommand cmd = new sqlcommand("addordertran", conn);
// cmd.commandtype = commandtype.storedprocedure;
// sqlparameter para1 = new sqlparameter("@country", country);
// para1.direction = parameterdirection.input; //参数方向 为输入参数
// cmd.parameters.add(para1);
// sqlparameter para2 = new sqlparameter("@adds", adds);
// para2.direction = parameterdirection.input;
// cmd.parameters.add(para2);
// sqlparameter para3 = new sqlparameter("@ynames", ynames);
// para3.direction = parameterdirection.input;
// cmd.parameters.add(para3);
// sqlparameter para4 = new sqlparameter("@pids", pids);
// para4.direction = parameterdirection.input;
// cmd.parameters.add(para4);
// sqlparameter para5 = new sqlparameter("@cellp", cellp);
// para5.direction = parameterdirection.input;
// cmd.parameters.add(para5);
// sqlparameter para6 = new sqlparameter("@cphone", cphone);
// para6.direction = parameterdirection.input;
// cmd.parameters.add(para6);
// sqlparameter para7 = new sqlparameter("@amounts", amounts);
// para7.direction = parameterdirection.input;
// cmd.parameters.add(para7);
// sqlparameter para8 = new sqlparameter("@cartnumber", cartnumber);
// para8.direction = parameterdirection.input;
// cmd.parameters.add(para8);
// sqlparameter parareturn = new sqlparameter("@return", sqldbtype.int);
// parareturn.direction = parameterdirection.returnvalue; //参数方向 为返回参数
// cmd.parameters.add(parareturn);
// cmd.executenonquery();
// object obj = parareturn;
// if (obj.tostring() == "0")
// {
// //存储过程执行失败
// }
// else
// {
// //成功
// }
//}
//#endregion
本文的数据库用的是sql server自带数据northwind
1.只返回单一记录集的存储过程
sqlconnection sqlconn = new sqlconnection(conn);
sqlcommand cmd = new sqlcommand();
// 设置sql连接
cmd.connection = sqlconn;
// 如果执行语句
cmd.commandtext = "categoriestest1";
// 指定执行语句为存储过程
cmd.commandtype = commandtype.storedprocedure;
sqldataadapter dp = new sqldataadapter(cmd);
dataset ds = new dataset();
// 填充dataset
dp.fill(ds);
// 以下是显示效果
gridview1.datasource = ds;
gridview1.databind();
存储过程categoriestest1
create procedure categoriestest1
as
select *
from categories
go
2. 没有输入输出的存储过程
sqlconnection sqlconn = new sqlconnection(conn);
sqlcommand cmd = new sqlcommand();
cmd.connection = sqlconn;
cmd.commandtext = "categoriestest2";
cmd.commandtype = commandtype.storedprocedure;
sqlconn.open();
// 执行并显示影响行数
label1.text = cmd.executenonquery().tostring();
sqlconn.close();
存储过程categoriestest2
create procedure categoriestest2 as
insert into dbo.categories
(categoryname,[description],[picture])
values ('test1','test1',null)
go
3. 有返回值的存储过程
sqlconnection sqlconn = new sqlconnection(conn);
sqlcommand cmd = new sqlcommand();
cmd.connection = sqlconn;
cmd.commandtext = "categoriestest3";
cmd.commandtype = commandtype.storedprocedure;
// 创建参数
idataparameter[] parameters = {
new sqlparameter("rval", sqldbtype.int,4)
};
// 将参数类型设置为 返回值类型
parameters[0].direction = parameterdirection.returnvalue;
// 添加参数
cmd.parameters.add(parameters[0]);
sqlconn.open();
// 执行存储过程并返回影响的行数
label1.text = cmd.executenonquery().tostring();
sqlconn.close();
// 显示影响的行数和返回值
label1.text += "-" + parameters[0].value.tostring() ;
存储过程categoriestest3
create procedure categoriestest3
as
insert into dbo.categories
(categoryname,[description],[picture])
values ('test1','test1',null)
return @@rowcount
go
4. 有输入参数和输出参数的存储过程
sqlconnection sqlconn = new sqlconnection(conn);
sqlcommand cmd = new sqlcommand();
cmd.connection = sqlconn;
cmd.commandtext = "categoriestest4";
cmd.commandtype = commandtype.storedprocedure;
// 创建参数
idataparameter[] parameters = {
new sqlparameter("@id", sqldbtype.int,4) ,
new sqlparameter("@categoryname", sqldbtype.nvarchar,15) ,
};
// 设置参数类型
parameters[0].direction = parameterdirection.output; // 设置为输出参数
parameters[1].value = "testcategoryname";
// 添加参数
cmd.parameters.add(parameters[0]);
cmd.parameters.add(parameters[1]);
sqlconn.open();
// 执行存储过程并返回影响的行数
label1.text = cmd.executenonquery().tostring();
sqlconn.close();
// 显示影响的行数和输出参数
label1.text += "-" + parameters[0].value.tostring() ;
存储过程categoriestest4
create procedure categoriestest4
@id int output,
@categoryname nvarchar(15)
as
insert into dbo.categories
(categoryname,[description],[picture])
values (@categoryname,'test1',null)
set @id = @@identity
go
5. 同时具有返回值、输入参数、输出参数的存储过程
sqlconnection sqlconn = new sqlconnection(conn);
sqlcommand cmd = new sqlcommand();
cmd.connection = sqlconn;
cmd.commandtext = "categoriestest5";
cmd.commandtype = commandtype.storedprocedure;
// 创建参数
idataparameter[] parameters = {
new sqlparameter("@id", sqldbtype.int,4) ,
new sqlparameter("@categoryname", sqldbtype.nvarchar,15) ,
new sqlparameter("rval", sqldbtype.int,4)
};
// 设置参数类型
parameters[0].direction = parameterdirection.output; // 设置为输出参数
parameters[1].value = "testcategoryname"; // 给输入参数赋值
parameters[2].direction = parameterdirection.returnvalue; // 设置为返回值
// 添加参数
cmd.parameters.add(parameters[0]);
cmd.parameters.add(parameters[1]);
cmd.parameters.add(parameters[2]);
sqlconn.open();
// 执行存储过程并返回影响的行数
label1.text = cmd.executenonquery().tostring();
sqlconn.close();
// 显示影响的行数,输出参数和返回值
label1.text += "-" + parameters[0].value.tostring() + "-" + parameters[2].value.tostring();
存储过程categoriestest5
create procedure categoriestest5
@id int output,
@categoryname nvarchar(15)
as
insert into dbo.categories
(categoryname,[description],[picture])
values (@categoryname,'test1',null)
set @id = @@identity
return @@rowcount
go
6. 同时返回参数和记录集的存储过程
sqlconnection sqlconn = new sqlconnection(conn);
sqlcommand cmd = new sqlcommand();
cmd.connection = sqlconn;
cmd.commandtext = "categoriestest6";
cmd.commandtype = commandtype.storedprocedure;
// 创建参数
idataparameter[] parameters = {
new sqlparameter("@id", sqldbtype.int,4) ,
new sqlparameter("@categoryname", sqldbtype.nvarchar,15) ,
new sqlparameter("rval", sqldbtype.int,4) // 返回值
};
// 设置参数类型
parameters[0].direction = parameterdirection.output; // 设置为输出参数
parameters[1].value = "testcategoryname"; // 给输入参数赋值
parameters[2].direction = parameterdirection.returnvalue; // 设置为返回值
// 添加参数
cmd.parameters.add(parameters[0]);
cmd.parameters.add(parameters[1]);
cmd.parameters.add(parameters[2]);
sqldataadapter dp = new sqldataadapter(cmd);
dataset ds = new dataset();
// 填充dataset
dp.fill(ds);
// 显示结果集
gridview1.datasource = ds.tables[0];
gridview1.databind();
label1.text = "";
// 显示输出参数和返回值
label1.text += parameters[0].value.tostring() + "-" + parameters[2].value.tostring();
存储过程categoriestest6
create procedure categoriestest6
@id int output,
@categoryname nvarchar(15)
as
insert into dbo.categories
(categoryname,[description],[picture])
values (@categoryname,'test1',null)
set @id = @@identity
select * from categories
return @@rowcount
go
7. 返回多个记录集的存储过程
sqlconnection sqlconn = new sqlconnection(conn);
sqlcommand cmd = new sqlcommand();
cmd.connection = sqlconn;
cmd.commandtext = "categoriestest7";
cmd.commandtype = commandtype.storedprocedure;
sqldataadapter dp = new sqldataadapter(cmd);
dataset ds = new dataset();
// 填充dataset
dp.fill(ds);
// 显示结果集1
gridview1.datasource = ds.tables[0];
gridview1.databind();
// 显示结果集2
gridview2.datasource = ds.tables[1];
gridview2.databind();
存储过程categoriestest7
create procedure categoriestest7
as
select * from categories
select * from categories
go
更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结》
希望本文所述对大家c#程序设计有所帮助。
本文实例讲述了c#调用存储过程的方法。分享给大家供大家参考,具体如下:
01 |
create procedure [dbo].[getnamebyid] |
02 |
@studentid varchar (8), |
03 |
@studentname nvarchar(50) output |
06 |
select @studentname=studentname from student |
07 |
where studentid=@studentid |
01 |
using (sqlconnection conn = new sqlconnection(connstr)) |
05 |
sqlcommand cmd = new sqlcommand( "getnamebyid" , conn); |
06 |
cmd.commandtype = commandtype.storedprocedure; |
07 |
cmd.parameters.addwithvalue( "@studentid" , "09888888" ); |
08 |
sqlparameter paroutput =cmd.parameters.add( "@studentname" , sqldbtype.nvarchar, 50); |
09 |
paroutput.direction = parameterdirection.output; |
10 |
sqlparameter parreturn = new sqlparameter( "@return" , sqldbtype. int ); |
11 |
parreturn.direction = parameterdirection.returnvalue; |
12 |
cmd.parameters.add(parreturn); |
14 |
cmd.executenonquery(); |
15 |
messagebox.show(paroutput.value.tostring()); |
16 |
messagebox.show(parreturn.value.tostring()); |
18 |
catch (system.exception ex) |
20 |
messagebox.show(ex.message); |
01 |
create procedure addordertran |
02 |
@country nvarchar(100), |
04 |
@ynames nvarchar(100), |
07 |
@cphone nvarchar(100), |
08 |
@amounts nvarchar(100), |
09 |
@cartnumber nvarchar(100) |
13 |
insert into orders(order_country,order_adress,order_username,order_postid,cells,order_phone,total_pay,cartnumber,ispay) |
14 |
values (@country,@adds,@ynames,@pids,@cellp,@cphone,@amounts,@cartnumber, '0' ) |
16 |
insert into orders_item (ordernumber,productsid,products_color,products_price,order_qty,item_total) |
17 |
select @id,carts_item.productsid,carts_item.products_color,carts_item.products_price,carts_item.item_qty,carts_item.total_pay |
18 |
from carts_item where carts_item.cartnumber=@cartnumber |
19 |
delete carts_item where cartnumber=@cartnumber |
02 |
sqlparameter[] param = new sqlparameter[] |
04 |
new sqlparameter( "@country" ,country), |
05 |
new sqlparameter( "@adds" ,adds), |
06 |
new sqlparameter( "@ynames" ,ynames), |
07 |
new sqlparameter( "@pids" , pids), |
08 |
new sqlparameter( "@cellp" ,cellp), |
09 |
new sqlparameter( "@cphone" , cphone), |
10 |
new sqlparameter( "@amounts" ,amounts), |
11 |
new sqlparameter( "@cartnumber" ,cartnumber), |
12 |
new sqlparameter( "@return" ,sqldbtype. int ) |
14 |
param[8].direction = parameterdirection.returnvalue; |
15 |
mscl.sqlhelper.runprocedure( "addordertran" , param); |
16 |
object obj = param[8].value; |
本文的数据库用的是sql server自带数据northwind
1.只返回单一记录集的存储过程
01 |
sqlconnection sqlconn = new sqlconnection(conn); |
02 |
sqlcommand cmd = new sqlcommand(); |
04 |
cmd.connection = sqlconn; |
06 |
cmd.commandtext = "categoriestest1" ; |
08 |
cmd.commandtype = commandtype.storedprocedure; |
09 |
sqldataadapter dp = new sqldataadapter(cmd); |
10 |
dataset ds = new dataset(); |
14 |
gridview1.datasource = ds; |
存储过程categoriestest1
1 |
create procedure categoriestest1 |
2. 没有输入输出的存储过程
1 |
sqlconnection sqlconn = new sqlconnection(conn); |
2 |
sqlcommand cmd = new sqlcommand(); |
3 |
cmd.connection = sqlconn; |
4 |
cmd.commandtext = "categoriestest2" ; |
5 |
cmd.commandtype = commandtype.storedprocedure; |
8 |
label1.text = cmd.executenonquery().tostring(); |
存储过程categoriestest2
1 |
create procedure categoriestest2 as |
2 |
insert into dbo.categories |
3 |
(categoryname,[description],[picture]) |
4 |
values ( 'test1' , 'test1' , null ) |
3. 有返回值的存储过程
01 |
sqlconnection sqlconn = new sqlconnection(conn); |
02 |
sqlcommand cmd = new sqlcommand(); |
03 |
cmd. connection = sqlconn; |
04 |
cmd.commandtext = "categoriestest3" ; |
05 |
cmd.commandtype = commandtype.storedprocedure; |
07 |
idataparameter[] parameters = { |
08 |
new sqlparameter( "rval" , sqldbtype. int ,4) |
11 |
parameters[0].direction = parameterdirection.returnvalue; |
13 |
cmd.parameters. add (parameters[0]); |
16 |
label1.text = cmd.executenonquery().tostring(); |
19 |
label1.text += "-" + parameters[0].value.tostring() ; |
存储过程categoriestest3
1 |
create procedure categoriestest3 |
3 |
insert into dbo.categories |
4 |
(categoryname,[description],[picture]) |
5 |
values ( 'test1' , 'test1' , null ) |
4. 有输入参数和输出参数的存储过程
01 |
sqlconnection sqlconn = new sqlconnection(conn); |
02 |
sqlcommand cmd = new sqlcommand(); |
03 |
cmd.connection = sqlconn; |
04 |
cmd.commandtext = "categoriestest4" ; |
05 |
cmd.commandtype = commandtype.storedprocedure; |
07 |
idataparameter[] parameters = { |
08 |
new sqlparameter( "@id" , sqldbtype. int ,4) , |
09 |
new sqlparameter( "@categoryname" , sqldbtype.nvarchar,15) , |
12 |
parameters[0].direction = parameterdirection.output; |
13 |
parameters[1].value = "testcategoryname" ; |
15 |
cmd.parameters.add(parameters[0]); |
16 |
cmd.parameters.add(parameters[1]); |
19 |
label1.text = cmd.executenonquery().tostring(); |
22 |
label1.text += "-" + parameters[0].value.tostring() ; |
存储过程categoriestest4
1 |
create procedure categoriestest4 |
3 |
@categoryname nvarchar(15) |
5 |
insert into dbo.categories |
6 |
(categoryname,[description],[picture]) |
7 |
values (@categoryname, 'test1' , null ) |
5. 同时具有返回值、输入参数、输出参数的存储过程
01 |
sqlconnection sqlconn = new sqlconnection(conn); |
02 |
sqlcommand cmd = new sqlcommand(); |
03 |
cmd. connection = sqlconn; |
04 |
cmd.commandtext = "categoriestest5" ; |
05 |
cmd.commandtype = commandtype.storedprocedure; |
07 |
idataparameter[] parameters = { |
08 |
new sqlparameter( "@id" , sqldbtype. int ,4) , |
09 |
new sqlparameter( "@categoryname" , sqldbtype.nvarchar,15) , |
10 |
new sqlparameter( "rval" , sqldbtype. int ,4) |
13 |
parameters[0].direction = parameterdirection. output ; // 设置为输出参数 |
14 |
parameters[1].value = "testcategoryname" ; // 给输入参数赋值 |
15 |
parameters[2].direction = parameterdirection.returnvalue; // 设置为返回值 |
17 |
cmd.parameters. add (parameters[0]); |
18 |
cmd.parameters. add (parameters[1]); |
19 |
cmd.parameters. add (parameters[2]); |
22 |
label1.text = cmd.executenonquery().tostring(); |
25 |
label1.text += "-" + parameters[0].value.tostring() + "-" + parameters[2].value.tostring(); |
存储过程categoriestest5
01 |
create procedure categoriestest5 |
03 |
@categoryname nvarchar(15) |
05 |
insert into dbo.categories |
06 |
(categoryname,[description],[picture]) |
07 |
values (@categoryname, 'test1' , null ) |
6. 同时返回参数和记录集的存储过程
01 |
sqlconnection sqlconn = new sqlconnection(conn); |
02 |
sqlcommand cmd = new sqlcommand(); |
03 |
cmd.connection = sqlconn; |
04 |
cmd.commandtext = "categoriestest6" ; |
05 |
cmd.commandtype = commandtype.storedprocedure; |
07 |
idataparameter[] parameters = { |
08 |
new sqlparameter( "@id" , sqldbtype. int ,4) , |
09 |
new sqlparameter( "@categoryname" , sqldbtype.nvarchar,15) , |
10 |
new sqlparameter( "rval" , sqldbtype. int ,4) |
13 |
parameters[0].direction = parameterdirection.output; |
14 |
parameters[1].value = "testcategoryname" ; |
15 |
parameters[2].direction = parameterdirection.returnvalue; |
17 |
cmd.parameters.add(parameters[0]); |
18 |
cmd.parameters.add(parameters[1]); |
19 |
cmd.parameters.add(parameters[2]); |
20 |
sqldataadapter dp = new sqldataadapter(cmd); |
21 |
dataset ds = new dataset(); |
25 |
gridview1.datasource = ds.tables[0]; |
29 |
label1.text += parameters[0].value.tostring() + "-" + parameters[2].value.tostring(); |
存储过程categoriestest6
01 |
create procedure categoriestest6 |
03 |
@categoryname nvarchar(15) |
05 |
insert into dbo.categories |
06 |
(categoryname,[description],[picture]) |
07 |
values (@categoryname, 'test1' , null ) |
09 |
select * from categories |
7. 返回多个记录集的存储过程
01 |
sqlconnection sqlconn = new sqlconnection(conn); |
02 |
sqlcommand cmd = new sqlcommand(); |
03 |
cmd.connection = sqlconn; |
04 |
cmd.commandtext = "categoriestest7" ; |
05 |
cmd.commandtype = commandtype.storedprocedure; |
06 |
sqldataadapter dp = new sqldataadapter(cmd); |
07 |
dataset ds = new dataset(); |
11 |
gridview1.datasource = ds.tables[0]; |
14 |
gridview2.datasource = ds.tables[1]; |
存储过程categoriestest7
1 |
create procedure categoriestest7 |
3 |
select * from categories |
4 |
select * from categories |
更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结》
希望本文所述对大家c#程序设计有所帮助。