使用三元语法将参数设置为DBNull.Value会出错吗? [英] Setting parameter to DBNull.Value using ternary syntax gives error?

查看:180
本文介绍了使用三元语法将参数设置为DBNull.Value会出错吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来设置一个参数,该参数将在INSERT语句中用于设置SQL Server数据库中的VARCHAR列。我的值对象(名为ilo)具有一个名为Description的属性,该属性被初始化为String.Empty,然后被设置为从XML读取的某个值,或者如果该XML元素为空,则保持为String.Empty。 >

因此,在插入数据库时​​,如果该属性仍设置为String.Empty,我希望它插入一个空值。

  database.AddInParameter(cmd, @description,DbType.String,
(ilo.Description.Equals(string.Empty))?
DBNull.Value:
ilo.Description);

所以基本上我是说,如果ilo.Description等于string.empty,则将参数设置为DBNull .value,否则将其设置为ilo.Description。



这会在Visual Studio中产生以下错误...



Error 141无法确定条件表达式的类型,因为'System.DBNull'和'string'之间没有隐式转换



为什么?



奇怪的是,我可以无错误地执行以下操作,这与使用上述内联条件语法完全一样!

  if(ilo.Description.Equals(string.Empty))
{
database.AddInParameter( cmd, @ description,DbType.String,DBNull.Value);
}
else
{
database.AddInParameter(cmd, @description,DbType.String,ilo.Description);
}

我搜索了其他帖子,但发现下面的帖子,但没有找到真的回答了我的问题。





我对WHY更加感兴趣,因为显而易见的解决方法是仅使用if / else语句而不是内联(三进制)



在此链接上有一个答案,但我希望有一个更好的解释,因为对我来说,这似乎不可行;我会称它为错误!



http://msdn.microsoft.com/zh-cn/library/ty67wk28.aspx

解决方案

这是人们在使用条件运算符时遇到的常见错误。要解决此问题,只需将一个或两个结果转换为一个通用的基本类型。

  ilo.Description.Equals(string.Empty)
吗? (object)DBNull.Value
:ilo.Description

问题在错误中揭示您看到的消息。


由于在'System.DBNull'和'string'之间没有隐式转换,因此无法确定条件表达式的类型


字符串不是DBNull,DBNull不是字符串。因此,编译器无法确定表达式的类型。通过使用强制转换为通用基本类型(在这种情况下为 object ),可以创建一种方案,使编译器可以确定该字符串也可以转换为对象,因此可以将表达式的类型确定为对象,这也很适合您的代码行还希望将其作为DbParameter参数。


I have the following bit of code to set a parameter that will be used in an INSERT statement to set a VARCHAR column in a SQL Server database. My value object (named ilo) has a property called Description that gets initialized to String.Empty, and then either gets set to some value read from XML, or if that XML element is empty it just stays as String.Empty.

So when inserting into the database, if the property is still set to String.Empty, I'd like to have it insert a null value.

database.AddInParameter(cmd, "@description", DbType.String, 
                           (ilo.Description.Equals(string.Empty)) ?
                            DBNull.Value :
                            ilo.Description);

So basically I'm saying, if ilo.Description equals string.empty, set the parameter to DBNull.Value, otherwise set it to ilo.Description.

This gives the following error in Visual Studio...

Error 141 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'

Why?

The curious part is that I can do the following with no error, which should be exactly the same thing as using inline conditional syntax like above!?!

if(ilo.Description.Equals(string.Empty))
{
    database.AddInParameter(cmd, "@description", DbType.String, DBNull.Value);
}
else
{
    database.AddInParameter(cmd, "@description", DbType.String, ilo.Description);
}

I searched other posts, and found the one below, but it doesn't really answer my question.

EntLib Way to Bind "Null" Value to Parameter

I'm more interested in WHY, because the obvious workaround is to just use an if/else statement instead of the inline (ternary) syntax?

There's sort of an answer at this link, but I'd like a better explanation because it seems to be BS to me that this doesn't work; I'd call it a bug!

http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

解决方案

This is a common error people receive when using the conditional operator. To fix it, simply cast one or both of the results to a common base type.

ilo.Description.Equals(string.Empty) 
     ? (object)DBNull.Value 
     : ilo.Description

The issue is revealed in the error message you saw.

Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'

A string is not a DBNull, and a DBNull is not a string. Therefore, the compiler cannot determine the type of the expression. By using a cast to a common base type (in this case, object), you create a scenario where the compiler can then determine that string is also convertible to object, so the type of the expression can be determined as object, which also nicely fits with what your line of code also expects as the DbParameter argument.

这篇关于使用三元语法将参数设置为DBNull.Value会出错吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆