将 null 分配给 SqlParameter
以下代码给出错误 - “没有从 DBnull 到 int 的隐式转换。”
SqlParameter[] parameters = new SqlParameter[1];
SqlParameter planIndexParameter = new SqlParameter("@AgeIndex", SqlDbType.Int);
planIndexParameter.Value = (AgeItem.AgeIndex== null) ? DBNull.Value : AgeItem.AgeIndex;
parameters[0] = planIndexParameter;
The following code gives an error - "No implicit conversion from DBnull to int."
SqlParameter[] parameters = new SqlParameter[1];
SqlParameter planIndexParameter = new SqlParameter("@AgeIndex", SqlDbType.Int);
planIndexParameter.Value = (AgeItem.AgeIndex== null) ? DBNull.Value : AgeItem.AgeIndex;
parameters[0] = planIndexParameter;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
问题是
?:
运算符无法确定返回类型,因为您返回的是int
值或 DBNull 类型值,这两者不兼容。当然,您可以将 AgeIndex 的实例转换为
object
类型,这将满足?:
要求。您可以使用
??
空合并运算符,如下所示这是来自 MSDN 文档,用于解释问题的
?:
运算符The problem is that the
?:
operator cannot determine the return type because you are either returning anint
value or a DBNull type value, which are not compatible.You can of course cast the instance of AgeIndex to be type
object
which would satisfy the?:
requirement.You can use the
??
null-coalescing operator as followsHere is a quote from the MSDN documentation for the
?:
operator that explains the problem接受的答案建议使用强制转换。然而,大多数 SQL 类型都有一个特殊的 Null 字段,可用于避免这种转换。
例如,
SqlInt32.Null
“表示可以分配给 SqlInt32 类的此实例的 DBNull。”
The accepted answer suggests making use of a cast. However, most of the SQL types have a special Null field which can be used to avoid this cast.
For example,
SqlInt32.Null
"Represents a DBNull that can be assigned to this instance of the SqlInt32 class."您需要在 SQLCommand 中将
DBNull.Value
作为空参数传递,除非在存储过程中指定了默认值(如果您正在使用存储过程)。最好的方法是在执行查询之前为任何缺少的参数分配DBNull.Value
,然后使用 foreach 即可完成这项工作。否则更改此行:
如下:
因为您不能在条件语句中使用不同类型的值,因为 DBNull 和 int 彼此不同。希望这会有所帮助。
You need pass
DBNull.Value
as a null parameter within SQLCommand, unless a default value is specified within stored procedure (if you are using stored procedure). The best approach is to assignDBNull.Value
for any missing parameter before query execution, and following foreach will do the job.Otherwise change this line:
As follows:
Because you can't use different type of values in conditional statement, as DBNull and int are different from each other. Hope this will help.
使用一行代码,尝试以下操作:
With one line of code, try this:
如果使用条件(三元)运算符,编译器需要在两种类型之间进行隐式转换,否则会出现异常。
因此,您可以通过将两者之一强制转换为
System.Object
来修复它:但是由于结果并不是很漂亮,并且您始终必须记住此转换,因此您可以使用这样的扩展方法:
然后您可以使用这个简洁的代码:
If you use the conditional(ternary) operator the compiler needs an implicit conversion between both types, otherwise you get an exception.
So you could fix it by casting one of both to
System.Object
:But since the result is not really pretty and you always have to remember this casting, you could use such an extension method instead:
Then you can use this concise code:
试试这个:
Try this:
在我看来,更好的方法是使用 SqlCommand 类:
In my opinion the better way is to do this with the Parameters property of the SqlCommand class:
你可以做这样的事情。这里 startDate 和 endDate 是可空日期时间参数
you can do something like this. Here startDate and endDate are nullable datetime param
试试这个:
换句话说,如果参数为空,则不要将其发送到存储过程(当然,假设存储过程接受问题中隐含的空参数)。
Try this:
In other words, if the parameter is null just don't send it to your stored proc (assuming, of course, that the stored proc accepts null parameters which is implicit in your question).
考虑使用可用的 Nullable(T) 结构。它只允许您设置值(如果有的话),并且您的 SQL Command 对象将识别可为空值并进行相应处理,而不会造成任何麻烦。
Consider using the Nullable(T) structure available. It'll let you only set values if you have them, and your SQL Command objects will recognize the nullable value and process accordingly with no hassle on your end.
一个简单的扩展方法是:
A simple extension method for this would be:
我使用一个带有空检查的简单方法。
I use a simple method with a null check.
我的代码,在实际项目中工作
在创建sql参数之前先查看三元运算符
这对我来说是最好的方法,没有问题:
My code, working in real project
Look the ternary operator beafore make the sqlparameter
this is the best way for me, withou problems:
尝试这样的事情:
try something like this:
我就是这样解决的
I'm solving like that.
这就是我简单做的事...
This is what I simply do...
改进空合并运算符的使用 ??为了在我的示例中管理空字符串,我将常规三元 ?: 与空合并运算符 ?? 混合。希望我的建议有用。
To improve the usage of the null-coalescing operator ?? to manage empty strings in my example I mixed the regular ternary ?: to the null-coalescing operator ??. Hope my suggestion is useful.