繁体   English   中英

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

[英]Check if VB.net dataset table exists

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

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

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

我如何在VB中检查表是否存在,而不是我的应用程序出错,我只希望表不存在时不执行任何操作。

我也尝试过

If ds is nothing

任何帮助,不胜感激。

如果你不知道,如果DataSet进行初始化:

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

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM