为什么如果我使用输出参数创建此存储过程,我收到以下错误:sp_DTS_InsertLSRBatch expects parameter @ErrorMsg which was notsupplied存储过程代码:set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER PROCEDURE...

为什么如果我使用输出参数创建此存储过程,我收到以下错误:
sp_DTS_InsertLSRBatch expects parameter @ErrorMsg which was not
supplied
存储过程代码:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_DTS_InsertLSRBatch]
@LSRNbr varchar(10),
@BatchNbr varchar(10),
@ErrorMsg varchar(20) output
AS
BEGIN
SET NOCOUNT ON;
if not exists(select *
from tblDTS_LSRBatch (nolock)
where LSRNbr=@LSRNbr and BatchNbr=@BatchNbr)
begin
-- check if BatchNbr exists under another LSR
-- if not add (LSR, BatchNbr) else error
if not exists(select *
from tblDTS_LSRBatch (nolock)
where BatchNbr=@BatchNbr)
insert into tblDTS_LSRBatch (LSRNbr,BatchNbr) values (@LSRNbr, @BatchNbr)
else
set @ErrorMsg = 'Batch dif LSR'
end
END
C#代码:
SqlConnection conn = new SqlConnection(ConnStr);
try
{
conn.Open();
for (int i = 0; i <= lbxBatch.Items.Count - 1; i++)
{
SqlCommand cmd = new SqlCommand("sp_DTS_InsertLSRBatch", conn);
cmd.Parameters.Add(new SqlParameter("@LSRNbr", txtLSR.Text));
cmd.Parameters.Add(new SqlParameter("@BatchNbr", lbxBatch.Items[i].ToString()));
//Output parameter "ErrorMsg"
SqlParameter pErrorMsg = new SqlParameter("@ErrorMsg", SqlDbType.VarChar, 20);
pErrorMsg.Direction = ParameterDirection.Output;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery(); <--- ERROR
解决方法:
在您的代码中,您尚未添加pErrorMsg参数.添加此行:
cmd.Parameters.Add(pErrorMsg);
织梦狗教程
本文标题为:sql server和c#中的输出参数问题


基础教程推荐
猜你喜欢
- C#实现支付宝沙箱支付的项目实践 2023-06-05
- C#中的串口通信SerialPort详解 2023-05-16
- C#位运算符的基本用法介绍 2023-07-04
- C#中AutoResetEvent控制线程用法小结 2023-06-21
- 【C#基础】Substring截取字符串的方法小结(推荐) 2023-01-22
- c# – 从中获取数据然后返回Windows剪贴板 2023-09-20
- 使用C#实现Windows组和用户管理的示例代码 2023-03-28
- Linux下安装SkyWalking 6.x版本 以及.NETCore项目集成 2023-09-28
- C#中三种Timer计时器的详细用法 2023-06-08
- C# Color.FromArgb()及系统颜色对照表一览 2023-03-28