[英]Output parameter issue in sql server and c#
Why if I have this stored procedure created with an output parameter, I'm getting the following error: 为什么如果我使用输出参数创建此存储过程,我收到以下错误:
sp_DTS_InsertLSRBatch expects parameter @ErrorMsg which was not supplied sp_DTS_InsertLSRBatch需要未提供的参数@ErrorMsg
Stored procedure code: 存储过程代码:
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# code: 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
In your code you haven't added the pErrorMsg parameter. 在您的代码中,您尚未添加pErrorMsg参数。 Add this line: 添加此行:
cmd.Parameters.Add(pErrorMsg);
此外,在存储过程中,必须将@ErrorMsg sql输出变量设置为适当的值,如空字符串或SP代码的if条件部分中的双引号(“”),作为良好的编码实践。
您正在创建pErrorMsg参数,但是在哪里将其添加到命令中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.