使用 VB.NET 从 Access 数据库中的表中获取列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18673858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:59:15  来源:igfitidea点击:

get column names from a table in an Access database using VB.NET

vb.netms-accessms-access-2010

提问by m hanif

How can I get all column names from a table in an Access database using VB.NET? With some articles on the Internet that I've tried, the result given is just the database schema. Is there any solution for this?

如何使用 VB.NET 从 Access 数据库中的表中获取所有列名?网上有一些我试过的文章,给出的结果只是数据库模式。有什么解决办法吗?

回答by Zev Spitz

You need to pass the name of the collection you are interested in to the GetSchemamethod. In the case of the columns collection, you also need to pass in an array of strings to filter the returned values.

您需要将您感兴趣的集合的名称传递给该GetSchema方法。对于列集合,还需要传入一个字符串数组来过滤返回值。

Dim connectionString = csb2.ToString
Dim tableName = "Sales Reports"
Dim filterValues = {Nothing, Nothing, tableName, Nothing}

Using conn = New OleDbConnection(connectionString)
    conn.Open
    Dim columns = conn.GetSchema("Columns", filterValues)
    For Each row As DataRow In columns.Rows
        Console.WriteLine("{0,-20}{1}",row("column_name"),row("data_type"))
    Next
End Using

See here.

这里