C#中调用存储过程:带输入输出参数

本文介绍了一种使用C#调用SQL Server存储过程的方法,并演示了如何通过存储过程传递输入参数及获取输出参数的具体实现。文章中展示了创建SqlCommand对象、设置CommandType为存储过程、指定存储过程名称、添加输入输出参数并执行存储过程的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using (SqlConnection conn = new SqlConnection(this.GetConnectionString(this.WMPDBName)))
{
SqlCommand cmd = conn.CreateCommand();

cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "存储过程名称";

////输出参数
cmd.Parameters.Add(new SqlParameter("@DeleteTotal", System.Data.SqlDbType.Int));
cmd.Parameters["@DeleteTotal"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(new SqlParameter("@InsertTotal", System.Data.SqlDbType.Int));
cmd.Parameters["@InsertTotal"].Direction = System.Data.ParameterDirection.Output;

//// 输入参数【方式一】
cmd.Parameters.Add(new SqlParameter("@PageIndex", System.Data.SqlDbType.Int));
cmd.Parameters["@PageIndex"].Direction = System.Data.ParameterDirection.Input;
cmd.Parameters["@PageIndex"].Value = 1;

//// 输入参数【方式二】
cmd.Parameters.Add(new SqlParameter("@PageIndex", 1));
conn.Open();
cmd.ExecuteNonQuery();
int.TryParse(cmd.Parameters["@DeleteTotal"].Value.ToString(), out deleteTotal);
int.TryParse(cmd.Parameters["@InsertTotal"].Value.ToString(), out insertTotal);
}

转载于:https://www.cnblogs.com/laopo/p/4473458.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值