1

下面的代码返回给定表(“Employee”)的字段,但我需要返回给定数据库中所有表的字段,这可能吗?我的假设是一个 For 循环,它循环数据库中的表并打印相应的字段,但我的努力似乎是徒劳的

Public Sub getDbFields()

    Dim i As Integer
    Dim dbcon As New System.Data.OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & dblocation & _
                                               "\" & dbname)
    Try

        dbcon.Open()
        dbDt = dbcon.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Columns, New Object() _
                                         {Nothing, Nothing, "Employee", Nothing})

        For i = 0 To dbDt.Rows.Count - 1
            'compile lbtables with a list of available tables from the database
            newLine()
            frmMain.lstTables.Items.Add(dbDt.Rows(i)!COLUMN_NAME.ToString())
        Next

    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString(), "Data Load Error", MessageBoxButtons.OK,
            MessageBoxIcon.Exclamation)
    End Try

End Sub

该例程将从选择一个复选框中触发

4

1 回答 1

0

这将返回数据库中的所有列

Using con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + 
                          "Data Source=" + dblocation + "\" + dbname)
    con.Open()
    Dim schema as DataTable = con.GetSchema("COLUMNS")
    Dim dr as DataRow
    For Each dr in schema.Rows
        Dim tablename as string = dr("TABLE_NAME").ToString()    
        if Not tablename.StartsWith("MSys") then
            Console.WriteLine(dr("TABLE_NAME").ToString() + " " + dr("COLUMN_NAME").ToString())
        End if        
    Next    
End Using

请注意,在 vb.net 中不允许使用 bang (!) 语法。
如果您更改,您的代码也可以工作

 dbDt = dbcon.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Columns, New Object() _ 
                                          {Nothing, Nothing, Nothing, Nothing}) 

这条线

frmMain.lstTables.Items.Add(dbDt.Rows(i)("COLUMN_NAME").ToString()) 
于 2012-08-22T13:05:58.383 回答