vb.net 如何在vb中创建条件来检查数据表项是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37203648/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:52:17  来源:igfitidea点击:

How to create a condition to check if the datatable item is null in vb

vb.netdbnull

提问by Johns Yong

Now I want if the dt.rows(i).item(0)is null, then some code..

现在我想要如果dt.rows(i).item(0)为空,那么一些代码..

This is my code:

这是我的代码:

If dtpay.Rows(i).Item(23).ToString Is Nothing Then
  GoTo finalline
End If

But seems like the code is not working.. Thanks a lot for your concern :D

但似乎代码不起作用..非常感谢您的关注:D

回答by Ian

You can use GetType()to check if an object is DBNullin VB.Net:

您可以使用GetType()来检查对象是否DBNullVB.Net

If dtpay.Rows(i).Item(23).GetType() Is GetType(DBNull) Then
    'Do something
End If

That being said, the DBNullin your code above may also happen in the dtpay.Rows(i). Thus, check also where the DBNulloccurs.

话虽如此,DBNull上面代码中的 也可能发生在dtpay.Rows(i). 因此,还要检查DBNull发生的位置。

回答by Veeke

Nothing is not the same as Null. Nothing returns the default value for the type of the field (0 for numbers, "" for text,...)

没有什么与 Null 不同。没有返回字段类型的默认值(0 表示数字,“” 表示文本,...)

    If IsDBNull(dtpay.Rows(i).Item(23)) Then
        GoTo finalline
    End If

回答by Tim Schmelter

You should use DataRow.IsNull

你应该使用 DataRow.IsNull

If dtpay.Rows(i).IsNull(23) Then
  ' ..... '
End If

If it's a value type(like Integer) you can use the DataRow-extension method Field:

如果它是值类型(如Integer),您可以使用DataRow-extension 方法Field

Dim myNullableField As Int32? = dtpay.Rows(i).Field(Of Int32?)
If Not myNullableField.HasValue Then
  ' you get it's value via myNullableField.Value '
End If

回答by Beldi Anouar

Try just :

试试吧:

If dtpay.Rows(i).Item(23) Is Nothing Then
  GoTo finalline
End If

回答by Johns Yong

If dt.Rows(i).Item(23) Is Nothing OrElse dt.Rows(i).Item(23).ToString = "" OrElse dt.Rows(i).Item(23) = vbNull Then GoTo finalline End If

If dt.Rows(i).Item(23) Is Nothing OrElse dt.Rows(i).Item(23).ToString = "" OrElse dt.Rows(i).Item(23) = vbNull Then GoTo finalline End If