vb.net 如何检查数据表是否为空或没有
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10331568/
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
How to check if a Datatable is Null or Nothing
提问by lawphotog
How can I check if a DataTable
has never been set, meaning it will be Null
or Nothing
? I don't mean an empty DataTable
.
我如何检查 aDataTable
是否从未设置过,这意味着它将是Null
或Nothing
?我的意思不是空的DataTable
。
For example:
例如:
Dim dt As DataTable = TryCast(Session("dt"), DataTable)
If dt.Rows.Count <> 0 Then
'Do something !
End If
If Session("dt")
has never been set or is lost in memory for some reason, dt.Rows.Count <> 0
will throw this exception:
如果Session("dt")
从未设置或由于某种原因在内存中丢失,dt.Rows.Count <> 0
将抛出此异常:
Object reference not set to an instance of an object.
你调用的对象是空的。
回答by Tim Schmelter
Preferred:
首选:
If dt Is Nothing Then ...
or (VB6 like)
或(像VB6)
If IsNothing(dt) Then ...
回答by ain
If the value datatype is varbinary(MAX), use
如果值数据类型是 varbinary(MAX),请使用
if dt.rows(0).item(2) Is DBNull.Value then...