How can DBNull not equal DBNull

牧云@^-^@ 提交于 2019-12-22 04:48:08

问题


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!


回答1:


Try using

Convert.IsDBNull method.




回答2:


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) 



回答3:


may be such comparison helps

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

or

if (o is DBNull)


来源:https://stackoverflow.com/questions/2060217/how-can-dbnull-not-equal-dbnull

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!