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

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

How to check if a Datatable is Null or Nothing

vb.netdatatablenullreferenceexception

提问by lawphotog

How can I check if a DataTablehas never been set, meaning it will be Nullor Nothing? I don't mean an empty DataTable.

我如何检查 aDataTable是否从未设置过,这意味着它将是NullNothing?我的意思不是空的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 <> 0will 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 ...

IsNothing Function

IsNothing 函数

回答by ain

If the value datatype is varbinary(MAX), use

如果值数据类型是 varbinary(MAX),请使用

if dt.rows(0).item(2) Is DBNull.Value then...