如何从SQL数据库中检索所有表(不包括系统表)

时间:2014-03-05 14:10:00

标签: sql sql-server vb.net

目标

从SQL数据库中检索所有表,同时排除系统表。

Table retrieval

绿色表格通知我想要的表格,红色矩形是我要排除的表格(系统表格)


尝试

这是我试过的

Private Function GetTables(ByVal cnSql As String) As List(Of String)
    Dim lst_Tables As New List(Of String)
    Dim mycn As SqlConnection
    Dim myCmd As SqlDataAdapter
    Dim myData As New DataSet()

    mycn = New SqlConnection(cnSql) 'Properly connects to my database
    mycn.Open()
    myCmd = New SqlDataAdapter("SELECT * FROM INFORMATION_SCHEMA.tables ORDER BY TABLE_NAME", mycn)
    myCmd.Fill(myData)
    mycn.Close()

    For Each table As DataTable In myData.Tables
        For Each row As DataRow In table.Rows
            'Only fetch tables (ommit views)
            If row.ItemArray(3).ToString = "BASE TABLE" Then
                For Each col As DataColumn In table.Columns
                    If col.ToString() = "TABLE_NAME" Then
                        lst_Tables.Add(row(col).ToString())
                        Exit For
                    End If
                Next
            End If
        Next
    Next

    Return lst_Tables
End Function

不幸的是,这也会返回系统表...我怎么会不包括这些?

1 个答案:

答案 0 :(得分:3)

使用此查询代替您的查询:

SELECT * FROM sys.objects WHERE type = 'U'

SELECT * FROM sys.tables

您还可以在谓词中使用is_ms_shipped = 0