开发者

VB.NET - Any way to get a nice clean list of all of the column names from a mdb? (Access Database)

I have a table in a Access Database that I'm loading into a VB.NET program, and I want a way to get the name of each of the columns into a list of Strings. Googling has shown that this is much easier with a SQL database, but that doing so in Access is much more difficult.

I came across this question Is there a query that will return all of the column names in a Microsoft Access table? Which gives a way开发者_StackOverflow社区 to do it, but when I try the code it just populates my list with a bunch of "System.Collections.Generic.List'1[System.String]"

This is my adapted code. Any suggestions for a fix or a better way to do it?

Public Function GetColumns(ByRef database As String, ByRef table_name As String, ByRef columns As List(Of String)) As Integer
Dim com As OleDbConnection
        Try
            com = New OleDbConnection(database)
            com.Open()
        Catch ex As Exception
            Return 1
        End Try

        Dim restrictions As String() = New String() {Nothing, Nothing, table_name, Nothing}
        Dim dt As DataTable = com.GetSchema("Columns", restrictions)
        com.Close()

        For Each DR As DataRow In dt.Rows
            columns.Add(DR("Column_Name").ToString)
        Next

        For Each holding As String In columns
            Console.WriteLine(columns)
        Next
End Function

Edit: Hah, I hate when I make stupid mistakes elsewhere in the function and it makes me think the meat of the function isn't working write. My for each loop isn't printing the right string. Should be Console.WriteLine(holding)


Console.WriteLine(columns)

change to:

Console.WriteLine(holding)

Then possibly kick yourself in the head for not spotting it =D ;-)


Try changing

    For Each holding As String In columns
        Console.WriteLine(columns)
    Next

to

    For Each holding As String In columns
        Console.WriteLine(holding)
    Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜