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

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

问题描述

我有一个带有 DateTime 列的表该列可以有 NULL 值

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

现在我使用 ODBC 连接连接到数据库,并将值放入 .net/c# 中的 DataTable.

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.Equals 对象,而不将其转换为字符串.

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天全站免登陆