最好的方法来检查,如果列返回空值(从数据库到.NET应用程序) [英] Best way to check if column returns a null value (from database to .net application)

查看:215
本文介绍了最好的方法来检查,如果列返回空值(从数据库到.NET应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表有一个日期时间列 列可以有NULL值

I have a table with a DateTime column the column can have NULL values

现在我连接到数据库使用ODBC连接,并获得价值成.NET / C#中的数据表。

Now I connect to the database using an ODBC connection and get the value into a DataTable in .net / c#.

我能够通过去检查它是否为NULL

I am able to check it for NULL by going

if(String.IsNullOrEmpty(table.rows[0][0].ToString())
{
     //Whatever I want to do
}

时String.IsNullOrEmpty以检查空值的正确方法。

Is String.IsNullOrEmpty the correct way to check for null values.

推荐答案

使用的DBNull.Value。等于中的对象上,而不将其转换成字符串。

Use DBNull.Value.Equals on the object without converting it to a string.

下面是一个例子:

   if (! DBNull.Value.Equals(row[fieldName])) 
   {
      //not null
   }
   else
   {
      //null
   }

这篇关于最好的方法来检查,如果列返回空值(从数据库到.NET应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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