0

我正在尝试使用 jet SQL 查询从表中读取列名。数据表以 2002-2003 兼容模式保存在 ms-access 中。我已经为查询尝试了几种变体,但似乎无法获得正确的语法。

DatabaseConnection("SELECT Column_name" & " FROM " & tablename, "Read Recordset")

DatabaseConnection("select column_name from information_schema.columns where table_name = tabDataHourly", "read Recordset")

我有一个运行数据库连接的函数: Public Function DatabaseConnection(ByVal Query As String, ByVal Task As String) As String 'On Error GoTo Err

    cnnOLEDB = New OleDbConnection
    cnnOLEDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataDirectoryName & DatabaseFileName
    cnnOLEDB.Open()

    cmdOLEDB.Connection = cnnOLEDB
    cmdOLEDB.CommandText = Query

    Select Case Task
        Case "Read Recordset"
            rdrOLEDB = cmdOLEDB.ExecuteReader()
            DatabaseConnection = "Read Recordset"
        Case "Read Scalar"
            DatabaseConnection = cmdOLEDB.ExecuteScalar
        Case "Non-Query"
            cmdOLEDB.ExecuteNonQuery()
            DatabaseConnection = "Non-Query"
    End Select

    Exit Function
Err:
    MsgBox("Database connection error.")
    DatabaseConnection = "Error"


End Function

任何帮助将不胜感激。

4

2 回答 2

0
select column_name,* from information_schema.columns
where table_name = 'tabDataHourly'
order by ordinal_position
于 2012-09-27T13:40:04.857 回答
0

关于获取字段(列)名称的一些注意事项:

        Case "Read Recordset"
            rdrOLEDB = cmdOLEDB.ExecuteReader()
            DatabaseConnection = "Read Recordset"
            For i = 0 To rdrOLEDB.FieldCount - 1
                Console.Write(rdrOLEDB.GetName(i))
            Next
            Console.Read()
于 2012-09-27T14:08:28.713 回答