如何使用VB.NET中的SQL从特定数据库中检索Ms访问表名称

时间:2016-05-25 20:05:35

标签: c# vb.net ms-access ms-access-2010

每当我想从特定的Ms Access 2007数据库中选择表名时,我就会使用这个SQL请求:

SELECT name from msysobjects WHERE type=1 and flags=0

我的VB.NET源代码:

 Connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + _
                     "Data Source=" & Text1.Text
        Connection.Open()
        ' sql = "SELECT MSysObjects.Name AS table_name FROM MSysObjects"
        sql = "SELECT name from msysobjects WHERE Type = 1 And flags = 0"
        Dim commande As OleDbCommand
        commande = New OleDbCommand(sql, Connection)
        Dim dataReader As OleDbDataReader

        Try
            dataReader = commande.ExecuteReader()
            While (dataReader.Read())
                tableNameBox.Text = dataReader.GetString(0)
            End While

        Catch ex As Exception
            MsgBox("Problème de connection " & ex.Message())

        End Try

我总是得到这个例外:

  

没有'msysobjects'的读取权限

我的目的是在comboBox中显示表名 谢谢

1 个答案:

答案 0 :(得分:2)

您不需要直接访问系统表。您可以使用Using conn As New OleDbConnection(myConnectionString) conn.Open() Dim dt As DataTable = conn.GetSchema("TABLES", {Nothing, Nothing, Nothing, "TABLE"}) For Each dr As DataRow In dt.Rows Console.WriteLine(dr("TABLE_NAME")) Next End Using 方法检索该信息。例如,要检索"常规"的列表(非系统)表您可以使用它:

cd .git
touch index.lock

有关详细信息,请参阅

OleDbConnection.GetSchema Method (String, String())