简体   繁体   English

如何将DBNull分配给原始类型以方便数据库插入

[英]How to assign DBNull to a primitive type to facilitate Database insert

I have the following line in my code assigning a value to a datatable column. 我在代码中将以下行赋值给datatable列。 It could be null or a price. 它可以为null或价格。

double? Price;
price = range.Cells[i, j].Value2 == null ? Double.NaN : range.Cells[i,j].Value2;
row["Price"] = **(price != Double.NaN) ? price.Value** : (object)DBNull.Value;

VS 2010 produces a green squiggly line (the code within the pairs of stars) saying VS 2010产生一条绿色的波浪线(成对的星星中的代码)说

The expression type is always false since double can never be null 表达式类型始终为false,因为double不能为null

How can I get around this so that I can assign null for price in the database table if need be? 如何解决这个问题,以便在需要时可以为数据库表中的价格分配空值?

Using Double.NaN won't help you because NaN is not equal even to itself. 使用Double.NaN不会帮助您,因为NaN甚至Double.NaN自身。 It's not clear why you're doing this in two stages though - I'd suggest just: 目前尚不清楚为什么要分两个阶段执行此操作-我建议:

row["Price"] = range.Cells[i, j].Value2 ?? (object) DBNull.Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM