我有一个Microsoft Access数据库,我使用VB.NET连接到Jet数据库引擎.我想以编程方式获取特定表的所有列名称.
我想做相当于这个MS SQL语句:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = 'TableName'
Access中有可能吗?如果没有,我有什么选择获取列名?
我找到了一种使用.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