[英]SQL output parameters in C#
I'm trying to get a value from Microsoft SQL Server stored procedure
. 我正在尝试从Microsoft SQL Server
stored procedure
获取值。 Totally I need to return two values. 总共我需要返回两个值。 As you see below one of them is accessible by return and the other is accessible by a output parameter.
正如您在下面看到的,其中一个可以通过return访问,另一个可以通过输出参数访问。 I run this code in WPF(C#):
我在WPF(C#)中运行以下代码:
public int InsertCustomerEntranceLogWithOptions(String cardNumber,int buy, int score, out int logID)
{
SqlCommand command = new SqlCommand(
@"
--declare @id int, @logID int
exec @res = Insert_CustomerEntranceLog_WithOptions
@cardNumber, @buy, @score, @logID
--set @logID = @id",
myConnection);
command.CommandType = CommandType.Text;
command.Parameters.Add("@cardNumber", SqlDbType.NVarChar).Value = cardNumber;
command.Parameters.Add("@buy", SqlDbType.Int).Value = buy;
command.Parameters.Add("@score", SqlDbType.Int).Value = score;
command.Parameters.Add("@logID", SqlDbType.Int).Value = 0;
command.Parameters["@logID"].Direction = ParameterDirection.Output;
command.Parameters.Add("@res", SqlDbType.Int).Value = 0;
command.Parameters["@res"].Direction = ParameterDirection.Output;
int res = (int)RunNonQuery(command);
logID = (int)command.Parameters["logID"].Value;
return res;
}
and RunNonQuery Method is: 而RunNonQuery方法是:
public Object RunNonQuery(SqlCommand command, String returnVariableName = "@res")
{
Object result = null;
if (openConnection())
{
try
{
command.ExecuteNonQuery();
if (returnVariableName.Length != 0)
{
result = command.Parameters[returnVariableName].Value;
//command.Dispose();
}
else
{
//command.Dispose();
return 1;
}
}
catch (TimeoutException ex)
{
//Log
}
catch (Exception ex)
{
Utility.LogExceptionError(ex, 1, 1, 1);
//throw ex;
}
closeConnection();
}
return result;
}
I'm sure that my RunNonQuery
works properly. 我确定我的
RunNonQuery
可以正常工作。 I test it a lot. 我测试了很多。 But When I
InsertCustomerEntranceLogWithOptions
method, I Get this error : 但是当我使用
InsertCustomerEntranceLogWithOptions
方法时,出现此错误 :
An unhandled exception of type 'System.IndexOutOfRangeException'
occurred in System.Data.dll
Additional information: An SqlParameter with ParameterName 'logID'
is not contained by this SqlParameterCollection.
It seems that I do not add SqlParameter
but as you see LogID
is inserted. 似乎我没有添加
SqlParameter
但是如您所见,已插入LogID
。 what is wrong? 怎么了? I also remove comments in SQL command and then run it but steel I see the error.
我还删除了SQL命令中的注释,然后运行它,但是我看到了错误。
Seems like you forget the @ prefix for your sql parameter : 似乎您忘记了sql参数的@前缀:
logID = (int)command.Parameters["@logID"].Value
; logID = (int)command.Parameters["@logID"].Value
;
May be that your logID = (int)command.Parameters["logID"].Value;
可能是您的
logID = (int)command.Parameters["logID"].Value;
does not access the logId
because it should be named @logID
, as you've added it like: 不访问
logId
因为它应该命名为@logID
,就像您添加它一样:
command.Parameters.Add("@logID", SqlDbType.Int).Value = 0;
command.Parameters["@logID"].Direction = ParameterDirection.Output;
and '@' must be part of the parameter name - Is it necessary to add a @ in front of an SqlParameter name? 和'@'必须是参数名称的一部分- 是否有必要在SqlParameter名称前面添加@?
@
缺少,请像下面这样更新
logID = (int)command.Parameters["@logID"].Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.