vb.net 检查数据表值是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22341492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if Datatable Value is Null
提问by Codemunkeee
Basically, I just want to ask how to check a datatable value if it is null or not? I am looping through each cells on the table, and check if the value of a cell is null.
基本上,我只想问如何检查数据表值是否为空?我正在遍历表格上的每个单元格,并检查单元格的值是否为空。
I tried this
我试过这个
If Not dt.Rows(i)(j).value Is Nothing Then
MsgBox("cell empty")
End If
and
和
If Not dt.Rows(i)(j).value = "" Then
MsgBox("cell empty")
End If
but it is not working.
但它不起作用。
As of now, my datatable looks like this
截至目前,我的数据表看起来像这样
"e" means empty
“e”表示空
"ne" means not empty
“ne”表示不为空
"x" dont care
“x”不在乎
Col1 Col2 Col3 ...
ne x x ...
x e x ...
x x x ...
examples from the table above..
上表中的例子..
dt(0)(0) = ne
dt(0)(0) = ne
dt(1)(1) = e
dt(1)(1) = e
dt(0)(1) = either "e" or "ne"
dt(0)(1) = "e" 或 "ne"
PS, all the data in the datatable are all strings..
PS,datatable中的所有数据都是字符串..
how can I get the value if dt(1)(1) is empty? C# codes would be okay too..thanks
如果 dt(1)(1) 为空,我如何获得该值?C# 代码也可以..谢谢
EDIT:
编辑:
somehow, I've managed to solve my question by doing this, it met my requirement in my program so.. yea.
不知何故,我通过这样做设法解决了我的问题,它满足了我在程序中的要求,所以..是的。
If dt.Rows(i)(j).Value.ToString = "" Then
MsgBox("empty")
End If
thanks. Please close the thread.
谢谢。请关闭线程。
回答by Cory
回答by Lee Gary
http://msdn.microsoft.com/en-us/library/system.convert.isdbnull(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.convert.isdbnull(v=vs.110).aspx
you can use IsDBNull method
您可以使用 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);
}
回答by V-SHY
For VB.net
对于 VB.net
I found this, sorry I'm not sure what to check inside the IsDbNull (if wrong please forgive me) because I have no experience deal with database in vb.net.
我发现了这个,抱歉,我不确定在 IsDbNull 中检查什么(如果错误,请原谅我),因为我没有在 vb.net 中处理数据库的经验。
If wrong, please comment me to fix it, thanks.
如果错了,请评论我修复它,谢谢。
If NOT IsDbNull(dt.Rows(i)(j)) Then
MsgBox("cell non-empty")
ELSE
MsgBox("cell empty")
End If
OR check much more than only DbNull like accepted answer in this post
或者检查不仅仅是 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")
- If I want to check
IsDBNull(), I should putdt.Rows(i)(j)ordt.Rows(i)(j).Valueto check? - If I want to check
Is Nothing, I should putdt.Rows(i)(j)ordt.Rows(i)(j).Valueordt.Rows(i)(j).Value.ToStringto check?
- 如果我想检查
IsDBNull(),我应该放dt.Rows(i)(j)还是dt.Rows(i)(j).Value检查? - 如果我要检查
Is Nothing,我应该把dt.Rows(i)(j)或dt.Rows(i)(j).Value或dt.Rows(i)(j).Value.ToString检查?
thanks. Sorry for ask questions in question....
谢谢。抱歉问了有问题的问题......
回答by Manish Parakhiya
You can try following
您可以尝试以下
if(string.IsNullOrEmpty((string)dt.Rows[i][j].value))
{
MsgBox("cell empty");
}
else
{
MsgBox("cell is not empty")
}

