VB.NET - 从mdb获取所有列名称的干净清单的任何方法? (访问数据库)

时间:2011-06-16 14:17:34

标签: database vb.net ms-access

我在Access数据库中有一个表,我正在加载到VB.NET程序中,我想要一种方法将每个列的名称放入一个字符串列表中。谷歌搜索表明,使用SQL数据库会更容易,但在Access中这样做要困难得多。

我遇到了这个问题Is there a query that will return all of the column names in a Microsoft Access table?这提供了一种方法,但是当我尝试代码时,它只是用一堆“System.Collections.Generic.List'1 [System.String”填充我的列表。 ]“

这是我改编的代码。有关修复的建议或更好的方法吗?

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

编辑:哈,我讨厌当我在函数的其他地方犯愚蠢的错误时,它让我觉得函数的功能不起作用。我的每个循环都没有打印正确的字符串。应该是Console.WriteLine(持有)

2 个答案:

答案 0 :(得分:2)

Console.WriteLine(columns)

更改为:

Console.WriteLine(holding)

然后可能会因为没有发现它而将自己踢到脑袋= D; - )

答案 1 :(得分:1)

尝试更改

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

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