如何使用C#获取Access数据库中的列列表?

时间:2014-08-15 16:53:12

标签: c# ms-access

在运行时,如何获取Access中表格中的列列表?我不想要表格中的数据,只需要列名称和类型。

编辑:我忘了提到我使用C#。

2 个答案:

答案 0 :(得分:2)

您希望遍历表定义的fields属性。我列出了两种可以循环使用该物业的方法。

C#解决方案(使用System.DataSystem.Data.OleDb

https://stackoverflow.com/a/864382/2448686

VBA解决方案

Public Function GetTableDetails()
    Dim db As Database
    Dim td As TableDef

    Set db = CurrentDb
    Set td = db.TableDefs("tableName")

    Dim i As Long
    For i = 0 To td.Fields.Count - 1
        Debug.Print td.Fields(i).Type
    Next

    Dim f As Field
    For Each f In td.Fields
        Debug.Print f.Type
    Next
End Function

答案 1 :(得分:0)

使用VBA:

dim db as DAO.Database, rs as DAO.Recordset, f as DAO.Field
set db = currentDb()
set rs = db.openRecordset("yourTable", dbOpenDynaset, dbReadOnly)
with rs
    for each f in rs.Fields
        ' Simple example: print to the inmediate window
        debug.print f.Name
    next f
end with

未经测试: 使用Recordset对象代替TableDef对象:

dim db as DAO.Database, t as DAO.TableDef, f as DAO.Field
set db = currentDb()
set t = db.TableDefs("yourTable")
with t
    for each f in t.Fields
        debug.print f.name
    next f
end with