最好的方法来检查,如果列返回空值(从数据库到.NET应用程序)最好的、方法来、应用程序、数据库

2023-09-03 01:53:06 作者:为什么我吃德芙没有黑丝飘

我有一个表有一个日期时间列 列可以有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
   }