检查数据库单元格为空第一 [英] Check if a database cell is empty first
问题描述
我访问2007数据库通过C#中的MS Access,和我不断收到一个例外,每当我试图读取一个空单元格。
I am accessing an MS Access 2007 database through C#, and I keep getting an exception whenever I try to read an empty cell.
具体来说,我想读一个日期/时间细胞可能会或可能不会是空的。
我使用 OLE DB 以及填补一个DataSet。这些条件都不工作:
Specifically, I am trying to read a "Date/Time" cell that may or may not be empty. I am using OLE DB, and have filled a DataSet. None of these conditions work:
DataSet dataSet = GetDataSet();
DataRow row = dataSet.Tables[0].Rows[0];
DateTime time = new DateTime();
time = (DateTime)row[5]; // Exception thrown
如何检查电池是试图分配它之前是空的?这些工作都没有:
How to check if the cell is empty before trying to assign it? None of these work:
if(row[5] == null) ;
if(row[5] == DBNull) ;
if(row[5] == (String)"") ;
编辑:我应该提到:当我调试,它说,行[5]为系统.DBNull,但我得到一个错误,当我尝试:如果(行[5] ==的DBNull)。错误说的DBNull是一种类型,这是不是在给定的范围内有效。
I should have mentioned: When I debug, it says that row[5] equals "System.DBNull,", but I get an error when I try "if(row[5] == DBNULL)". The error says "DBNULL is a type, which is not valid in the given context".
推荐答案
您可以检查它像以下内容。
You can check it like the following.
if (row[5] == DBNull.Value)
{
// value is null
}
else if (String.IsNullOfEmpty(Convert.ToString(row[5]))
{
// value is still null
}
else
{
// value is not null here
}
这篇关于检查数据库单元格为空第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!