vb.net 检查VB.net数据集表是否存在

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

Check if VB.net dataset table exists

vb.netif-statementdataset

提问by user3580480

I have the following code to check if my table exists before proceeding

我有以下代码在继续之前检查我的表是否存在

        If ds.Tables(3).Rows.Count = 0 Then
            MsgBox("Nothing!!!!")
        Else
            DataGridView1.DataSource = ds.Tables(3)

The trouble is I keep getting the error "Cannot find table 3."

问题是我不断收到错误“找不到表 3”。

How in VB can I check if the table exists, rather than my application erroring I just want it to do nothing if the table doesn't exist.

我如何在 VB 中检查表是否存在,而不是我的应用程序出错,我只是希望它在表不存在的情况下什么都不做。

I have also tried

我也试过

If ds is nothing

Any help greatly appreciated.

非常感谢任何帮助。

回答by Chris

See if the dataset contains the table if you are unsure if it exists:

如果您不确定它是否存在,请查看数据集是否包含该表:

If mdsMyDataSet1.Tables.Contains("Table3") = True Then
   'Do Something with it
End If

回答by Tim Schmelter

If you don't know if the DataSetis initialized:

如果您不知道是否DataSet已初始化:

If ds IsNot Nothing Then
    ' ... '
End If

If you don't know if it contains fourtables(zero based indices):

如果您不知道它是否包含四个表(从零开始的索引):

If ds.Tables.Count >= 4 Then
        ' ... '
End If

So the final super safe version is:

所以最终的超级安全版本是:

If ds IsNot Nothing AndAlso ds.Tables.Count >= 4 Then
    Dim table As DataTable = ds.Tables(3)
End If

If you now also want to know if that table contains rows:

如果您现在还想知道该表是否包含行:

Dim isEmpty As Boolean = table.Rows.Count = 0