我有一个使用VB.NET连接到Jet数据库引擎的Microsoft Access数据库。我想以编程方式获取特定表的所有列名。
我想做相当于这个MS SQL语句:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = 'TableName'
这可以在Access中使用吗?如果没有,我有什么选择获取列名?
答案 0 :(得分:5)
我找到了一种使用.NET Connection类的GetSchema方法执行此操作的方法。
我编写了一个返回特定表的列名的方法。
Private Function GetColumnNamesInTable(ByVal connectionString As String, ByVal tableName As String) As List(Of String)
Dim connection As OleDbConnection = New OleDbConnection(connectionString)
Dim restrictions As String() = New String() {Nothing, Nothing, tableName, Nothing}
connection.Open()
Dim dataTable As DataTable = connection.GetSchema("Columns", restrictions)
connection.Close()
Dim returnList As List(Of String) = New List(Of String)
For Each dataRow As DataRow In dataTable.Rows
returnList.Add(dataRow("Column_Name"))
Next
Return returnList
End Function