我应该用什么来比较 DBNull ?使用 DBNull.Value 或 ToString().IsNullOrEmpty() [英] What should I use to compare DBNull ? Using DBNull.Value or ToString().IsNullOrEmpty()

查看:23
本文介绍了我应该用什么来比较 DBNull ?使用 DBNull.Value 或 ToString().IsNullOrEmpty()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用任何方法检查数据行上的 DBnull.

I can check for a DBnull on a data row using any of the methods.

通过使用

if(dr[0][0]==DBNull.Value)
//do somethin

或通过做

if(dr[0][0].ToString().IsNullOrEmpty())
//do something

在这两种情况下,我都会得到相同的结果.

In Both Cases I will be getting same result.

但是哪一个是concptually正确的方法.哪个会使用更少的资源

But Which one is conecptually right approach. Which was will use less resources

推荐答案

第一种方式有些正确.然而,更被接受的方式是:

The first way is somewhat correct. However, more accepted way is:

if ( dr[0][0] is DBNull )

而第二种方式肯定是不正确的.如果你使用第二种方式,你会在两种情况下得到 true:

And the second way is definitely incorrect. If you use the second way, you will get true in two cases:

  1. 您的值为 DBNull
  2. 您的值是一个空字符串

这篇关于我应该用什么来比较 DBNull ?使用 DBNull.Value 或 ToString().IsNullOrEmpty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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