DBNull 怎么会不等于 DBNull

发布于 2024-08-17 23:01:45 字数 355 浏览 1 评论 0原文

我有以下代码行,

if (DBNull.Value.Equals(o) || o != null)

其中 o 是 row.ItemArray 中的 object o 我不断收到错误 -->

Xml 类型“List of xdt:untypedAtomic”不支持从 Clr 类型“DBNull”到 Clr 类型“String”的转换。

我不明白的是,当我单步执行代码时,< code>if 应该捕获这个并执行我的替代操作,但它没有?

有人能给我一些启发吗?

谢谢你!

I have the following line of code

if (DBNull.Value.Equals(o) || o != null)

where o is object o in row.ItemArray I keep getting an error of -->

Xml type "List of xdt:untypedAtomic" does not support a conversion from Clr type "DBNull" to Clr type "String".

What I don't understand is that when I step through my code this if should be catching this and performing my alternate action but it does not?

Can someone please shed some light for me.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宫墨修音 2024-08-24 23:01:45

尝试使用

Convert.IsDBNull 方法。

Try using

Convert.IsDBNull method.

娇俏 2024-08-24 23:01:45

我认为你的问题是,事实上

DBNull.Value == null 
//is always false

DBNull 是一个特殊的类,用于比较从 dB 返回的值,因此如果你的数组包含两者,你实际上需要检查 null 条件和 DBNull.value 。

编辑:抱歉,仔细查看您的代码,您可能只需要反转“或”运算。如果 o == null 你的第一个语句将因你的异常而爆炸。尝试:

if (o != null || o == DBNull.Value) 

I think you problem is that in fact

DBNull.Value == null 
//is always false

The DBNull is a special class for comparisons on values returned from the dB so you actualy need to check for a null condition AND a DBNull.value if your array contains both.

EDIT: Sorry looking closer at your code you may just need to reverse your OR operation. If o == null your first statement will blow up with your exception. Try:

if (o != null || o == DBNull.Value) 
留一抹残留的笑 2024-08-24 23:01:45

可能这样的比较有帮助

if ( !o.GetType().Equals( DBNull.Value ) )

或者

if (o is DBNull)

may be such comparison helps

if ( !o.GetType().Equals( DBNull.Value ) )

or

if (o is DBNull)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文