SqlCommand.Parameters.AddWithValue 问题:过程或函数 X 需要参数 @Y,但未提供该参数
我对下面的代码有疑问。我将参数 (List
) 传递给执行以下代码的方法。
当它执行时,SQL Server 会抛出一个错误,指出该过程需要一个未提供的参数。我知道这个错误并理解它,当单步执行代码时,我可以看到 cmdExecuteReader 对象具有一组具有正确名称和值的参数。可能是什么问题?
public SqlDataReader ExecuteReader(string storedProcedure, List<SqlParameter> parameters = null)
{
SqlCommand cmdExecuteReader = new SqlCommand()
{
CommandType = System.Data.CommandType.Text,
Connection = conn,
CommandText = storedProcedure
};
if (parameters != null)
{
foreach (SqlParameter param in parameters)
{
cmdExecuteReader.Parameters.AddWithValue(param.ParameterName, param.Value);
}
}
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
return cmdExecuteReader.ExecuteReader();
}
I have a problem with the folowwing piece of code. I am passing a parameter (List<SqlParameter>
) to a method executing the following code.
When it executes SQL Server throws an error saying that the proc expects a parameter that was not provided. I know this error and understand it, and when stepping through the code I can see that the cmdExecuteReader
object has a collection of parameters with the correct name and value. What could be the problem?
public SqlDataReader ExecuteReader(string storedProcedure, List<SqlParameter> parameters = null)
{
SqlCommand cmdExecuteReader = new SqlCommand()
{
CommandType = System.Data.CommandType.Text,
Connection = conn,
CommandText = storedProcedure
};
if (parameters != null)
{
foreach (SqlParameter param in parameters)
{
cmdExecuteReader.Parameters.AddWithValue(param.ParameterName, param.Value);
}
}
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
return cmdExecuteReader.ExecuteReader();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何参数的
.Value
是否设置为null
?如果是这样,则不会发送它们。尝试:(注意与
DBNull.Value
的空合并)另外,请注意
AddWithValue
可能会影响您的查询计划重用,因为(对于字符串等)它使用值的长度。如果您需要最大性能,最好使用定义的大小手动设置参数。另请注意,传入列表中的某些参数可能可以是输入-输出、输出或结果。我很想用类似的东西来代替:
Is the
.Value
set tonull
for any of the parameters? If so, they aren't sent. Try:(note the null-coalescing with
DBNull.Value
)Also, note that
AddWithValue
may impact your query-plan re-use, as (for strings etc) it uses the length of the value. If you need maximum performance it is better to setup the parameter manually with the defined sizes.Also note that potentially some of the parameters in the incoming list could be input-output, output or result. I would be very tempted to substitute for something more like:
我做了你想做的事情,这里有一些例子:
我建议你看看 此处
并下载示例解决方案(其中包含 DAL 示例项目)
http://valueinjecter.codeplex.com/
I did the stuff that you are trying to do, here some examples:
I recommend you to look here
and to download the samples solution (where there is a DAL Sample project included)
http://valueinjecter.codeplex.com/