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

Check if VB.net dataset table exists

我有以下代码来检查我的 table 是否存在,然后再继续

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

问题是我一直收到错误 "Cannot find table 3."

我如何在 VB 中检查 table 是否存在,而不是我的应用程序错误我只是希望它在 table 不存在时什么也不做。

我也试过了

If ds is nothing

非常感谢任何帮助。

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

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

如果您不知道它是否包含 四个 tables(基于零的索引):

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

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

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

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

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

查看数据集是否包含 table 如果您不确定它是否存在:

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