检查Datatable Value是否为Null

本文关键字:Null 是否 Value Datatable 检查 | 更新日期: 2023-09-27 17:49:33

基本上,我只是想问如何检查一个数据表值,如果它是null或不是?我正在循环遍历表中的每个单元格,并检查单元格的值是否为null。

I tried this

If Not dt.Rows(i)(j).value Is Nothing Then
   MsgBox("cell empty")
End If

If Not dt.Rows(i)(j).value = "" Then
   MsgBox("cell empty")
End If

但是它不工作。

到目前为止,我的数据表是这样的

"e"表示空

"ne"表示不为空

"x"不关心


Col1 Col2 Col3 ... 
ne   x    x   ...
x    e    x   ...
x    x    x   ...

上表中的例子……

dt(0)(0) = ne

dt(1)(1) = e

dt(0)(1) = "e"或"ne"

PS, 数据表中的数据都是字符串

如果dt(1)(1)为空,我如何获得值?c#代码也可以…谢谢

编辑:

不知何故,我通过这样做解决了我的问题,它满足了我在程序中的要求,所以…是的。

If dt.Rows(i)(j).Value.ToString = "" Then
   MsgBox("empty")
End If

谢谢。请关闭线程

检查Datatable Value是否为Null

DBNull.Value比较。

foreach(DataRow row in myTable.Rows)
{
    object value = row["column"];
    if (value == DBNull.Value)
        // do something
    else
        // do something else
}

这里的另一个问题也有答案

http://msdn.microsoft.com/en-us/library/system.convert.isdbnull(v=vs.110).aspx

你可以使用IsDBNull方法

while (dr.Read())
{
  dr.GetValues(fieldValues);
  for (int fieldCounter = 0; fieldCounter < fieldCount; fieldCounter++)
  {
     if (Convert.IsDBNull(fieldValues[fieldCounter]))
        fieldValues[fieldCounter] = "NA";
  }
  grid.Rows.Add(fieldValues);

 }

您可以尝试以下操作

if(string.IsNullOrEmpty((string)dt.Rows[i][j].value))
{
  MsgBox("cell empty");
}
else
{
 MsgBox("cell is not empty")
}

For VB.net

我发现了这个,对不起,我不确定在IsDbNull(如果错了请原谅我)里面检查什么,因为我没有经验处理vb.net中的数据库。

如果错误,请评论我修复,谢谢。
If NOT IsDbNull(dt.Rows(i)(j)) Then
   MsgBox("cell non-empty")
ELSE
   MsgBox("cell empty")
End If

OR检查不仅仅是DbNull,就像在这篇文章中接受的答案

If IsDBNull(dt.Rows(i)(j)) OrElse dt.Rows(i)(j).Value.ToString Is Nothing OrElse dt.Rows(i)(j).Value.ToString = string.Empty Then
   MsgBox("cell empty")
  1. 如果我想检查IsDBNull(),我应该输入dt.Rows(i)(j)dt.Rows(i)(j).Value检查?
  2. 如果我想检查Is Nothing,我应该把dt.Rows(i)(j)或检查dt.Rows(i)(j).Value还是dt.Rows(i)(j).Value.ToString ?

谢谢。不好意思问问题....