C# ADO.NET:null 和 DbNull — 是否有更有效的语法?
我有一个 DateTime?
,我正在尝试使用 DbParameter
将其插入到字段中。 我正在像这样创建参数:
DbParameter datePrm = updateStmt.CreateParameter();
datePrm.ParameterName = "@change_date";
然后我想将 DateTime?
的值放入 dataPrm.Value
中,同时考虑 null
>s。
我最初以为我会很聪明:
datePrm.Value = nullableDate ?? DBNull.Value;
但是由于错误而失败
运算符“??” 不能应用于“System.DateTime?”类型的操作数 和“System.DBNull”
所以我想只有当第二个参数是第一个参数的不可空版本时才有效。 所以然后我去了:
datePrm.Value = nullableDate.HasValue ? nullableDate.Value : DBNull.Value;
但这也不起作用:
无法确定条件表达式的类型,因为“System.DateTime”和“System.DBNull”之间没有隐式转换
但我不想在这些类型之间进行转换!
到目前为止,我唯一能做的就是:
if (nullableDate.HasValue)
datePrm.Value = nullableDate.Value;
else
datePrm.Value = DBNull.Value;
这真的是我写这篇文章的唯一方法吗? 有没有办法让使用三元运算符的单行代码工作?
更新:我真的不明白为什么? 版本不起作用。 MSDN 说:
?? 如果左侧操作数不为空,则运算符返回左侧操作数,否则返回右侧操作数。
这正是我想要的!
更新2:嗯,最后很明显:
datePrm.Value = nullableDate ?? (object)DBNull.Value;
I've got a DateTime?
that I'm trying to insert into a field using a DbParameter
. I'm creating the parameter like so:
DbParameter datePrm = updateStmt.CreateParameter();
datePrm.ParameterName = "@change_date";
And then I want to put the value of the DateTime?
into the dataPrm.Value
while accounting for null
s.
I thought initially I'd be clever:
datePrm.Value = nullableDate ?? DBNull.Value;
but that fails with the error
Operator '??' cannot be applied to operands of type 'System.DateTime?' and 'System.DBNull'
So I guess that only works if the second argument is a non-nullable version of the first argument. So then I went for:
datePrm.Value = nullableDate.HasValue ? nullableDate.Value : DBNull.Value;
but that doesn't work either:
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and 'System.DBNull'
But I don't want to convert between those types!
So far the only thing I can get to work is:
if (nullableDate.HasValue)
datePrm.Value = nullableDate.Value;
else
datePrm.Value = DBNull.Value;
Is that really the only way I can write this? Is there a way to get a one-liner using the ternary operator to work?
Update: I don't really get why the ?? version doesn't work. MSDN says:
The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.
That's exactly what I want!
Update2: Well it was kind of obvious in the end:
datePrm.Value = nullableDate ?? (object)DBNull.Value;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
啊哈! 我找到了比@Trebz 更有效的解决方案!
Ah ha! I found an even more efficient solution than @Trebz's!
如果您使用 SQLServer,
System.Data.SqlTypes
命名空间包含一些实用程序类,可以避免烦人的类型转换。 例如,您可以这样写:
If you are using SQLServer, the
System.Data.SqlTypes
namespace contains some utility classes that avoid the annoying type casting. For example instead of this:you can write this:
如果你使用的话它会起作用
It would work if you used
如果您使用 C# 3.0,您可以创建一个扩展方法来轻松完成此操作:
If you're using C# 3.0 you can create an extension method to do this easy:
我认为您第二次尝试的错误是由于 nullableDate.Value 和 DBNull.Value 是不同的类型,并且三元运算符需要在两种情况下选择一种类型来返回。 我没有测试这个的环境,但我认为这应该适合你:
I think the error with your second attempt is due to nullableDate.Value and DBNull.Value being different types and the ternary operator needing to pick one type to return in both cases. I don't have the environment to test this but I think this should work for you:
我这样做的方式是,我有一个静态实用程序类,它只是遍历并检查参数值是否为 null,然后我设置该值来执行 DBNull。 我只是在调用执行之前执行此操作。
The way that I do it, is I have a static utility class that just goes through and checks to see if the parameter value is null, then i set the value to do DBNull. I just do that before i call the Execute.