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

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

问题描述

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



现在我使用ODBC连接连接数据库并获取值进入.net / c#中的DataTable。



我可以通过去

  if(String.IsNullOrEmpty(table.rows [0] [0] .ToString())
{
//无论我想要做什么
}

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

解决方案

使用 DBNull.Value.Equals 对象,而不将其转换为字符串。



以下是一个示例:



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


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

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

I am able to check it for NULL by going

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

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

解决方案

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

Here's an example:

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

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

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