如何使用SQL读取列名称

时间:2012-09-27 13:36:13

标签: sql vb.net visual-studio ms-access visual-studio-2005

我正在尝试使用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")

我有一个运行数据库连接的函数:      公共函数DatabaseConnection(ByVal Query As String,ByVal Task As String)As String         '错误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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

select column_name,* from information_schema.columns
where table_name = 'tabDataHourly'
order by ordinal_position

答案 1 :(得分: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()