没有合适的资源?快使用搜索试试~ 我知道了~
首页C#调用存储过程详解(带返回值、参数输入输出等)
C#调用存储过程详解(带返回值、参数输入输出等)
11 下载量 53 浏览量
更新于2023-05-03
收藏 58KB PDF 举报
本文实例讲述了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 SqlC
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/14914171/bg1.jpg)
C#调用存储过程详解调用存储过程详解(带返回值、参数输入输出等带返回值、参数输入输出等)
本文实例讲述了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),
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_38501916
- 粉丝: 1
- 资源: 935
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
会员权益专享
最新资源
- BSC绩效考核指标汇总 (2).docx
- BSC资料.pdf
- BSC绩效考核指标汇总 (3).pdf
- C5000W常见问题解决方案.docx
- BSC概念 (2).pdf
- ESP8266智能家居.docx
- ESP8266智能家居.pdf
- BSC概念 HR猫猫.docx
- C5000W常见问题解决方案.pdf
- BSC模板:关键绩效指标示例(财务、客户、内部运营、学习成长四个方面).docx
- BSC概念.docx
- BSC模板:关键绩效指标示例(财务、客户、内部运营、学习成长四个方面).pdf
- BSC概念.pdf
- 各种智能算法的总结汇总.docx
- BSC概念 HR猫猫.pdf
- bsc概念hr猫猫.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)