如何修复存储过程期望C#中未提供的返回参数?

时间:2018-10-02 13:02:57

标签: c# sql stored-procedures

我有一个名为“ sp_AddDevice”的存储过程,该存储过程接受以下变量:

@DeviceID AS NVARCHAR(8),
@Device_location AS NVARCHAR(40),
@Device_asset AS NVARCHAR(7),
@Device_name AS NVARCHAR(50),
@Device_manuf AS NVARCHAR(25),
@Device_model AS NVARCHAR(20),
@Device_serial AS NVARCHAR(40),
@Device_other AS NVARCHAR(20),
@Device_status AS SMALLINT,
@Device_type AS SMALLINT,
@EnteredBy AS VARCHAR(50),
@ChangedBy AS VARCHAR(50),
@errStatusOK INT OUTPUT

困扰我的是变量'@errStatusOK INT OUTPUT'

我正在像这样调用存储过程,但收到错误消息,未提供变量'@errStatusOK':

SqlCommand command = new SqlCommand(@"BCS_Verify.dbo.sp_AddDevice", con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@DeviceID", SqlDbType.NVarChar, 8).Value = deviceID;
command.Parameters.Add("@Device_location", SqlDbType.NVarChar, 40).Value = location;
command.Parameters.Add("@Device_asset", SqlDbType.NVarChar, 7).Value = deviceID.ToLower().TrimStart('b','c','s');
command.Parameters.Add("@Device_name", SqlDbType.NVarChar, 50).Value = type;
command.Parameters.Add("@Device_manuf", SqlDbType.NVarChar, 25).Value = brand;
command.Parameters.Add("@Device_model", SqlDbType.NVarChar, 20).Value = model;
command.Parameters.Add("@Device_serial", SqlDbType.NVarChar, 40).Value = serial;
command.Parameters.Add("@Device_other", SqlDbType.NVarChar, 20).Value = "N/A";
command.Parameters.Add("@Device_status", SqlDbType.SmallInt).Value = status;
command.Parameters.Add("@Device_type", SqlDbType.SmallInt).Value = 0;
command.Parameters.Add("@EnteredBy", SqlDbType.NVarChar, 50).Value = GlobalVar.Default.Username;
command.Parameters.Add("@ChangedBy", SqlDbType.NVarChar, 50).Value = GlobalVar.Default.Username;
var returnParameter = command.Parameters.Add("@errStatusOK", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
command.ExecuteNonQuery();
if (returnParameter.SqlValue.ToString() == "1") return true;
else return false;

enter image description here

我是否必须以其他方式定义返回变量,否则可能是问题所在?

3 个答案:

答案 0 :(得分:0)

尝试一下,也许足以将参数方向从结果更改为输出:

confirm("\u200f\u200f"+ message);

答案 1 :(得分:0)

我使用这样的东西:

command.Parameters.Add("@errStatusOK", SqlDbType.Int).Direction =
ParameterDirection.Output;

然后在执行查询后...

command.ExecuteNonQuery(); 
int iRetVal = Convert.ToInt32(commad.Parameters["@errStatusOK"].Value);

答案 2 :(得分:-1)

在执行存储过程时必须传递输出参数