C# 检索 MS Access 文件中的表列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1699897/
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-08-06 20:05:07  来源:igfitidea点击:

Retrieve List of Tables in MS Access File

c#.netdatabasems-accessmetadata

提问by Yaakov Ellis

If I can open a connection to an MS Access file in C#, how can I retrieve a list of the different tables that exist in the Access DB (and if possible, any meta-data associated with the tables)?

如果我可以在 C# 中打开到 MS Access 文件的连接,我如何检索 Access DB 中存在的不同表的列表(如果可能,还有与表关联的任何元数据)?

采纳答案by Yaakov Ellis

I just found the following solution from David Hayden

我刚刚从David Hayden找到了以下解决方案

// Microsoft Access provider factory
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");

DataTable userTables = null;
using (DbConnection connection = factory.CreateConnection()) {
  // c:\test\test.mdb
  connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test\test.mdb";
  // We only want user tables, not system tables
  string[] restrictions = new string[4];
  restrictions[3] = "Table";

  connection.Open();

  // Get list of user tables
  userTables = connection.GetSchema("Tables", restrictions);
}

List<string> tableNames = new List<string>();
for (int i=0; i < userTables.Rows.Count; i++)
    tableNames.Add(userTables.Rows[i][2].ToString());

回答by ParmesanCodice

Here's are some links:

以下是一些链接:

Here's a VB.NET snipit to get all the columns of a Access Table, I know it's not exactly what you're looking for, but a similar primciple apples when listing all the tables:

这是一个 VB.NET snipit,用于获取 Access 表的所有列,我知道这不是您要查找的内容,而是在列出所有表时类似的原始苹果:

Dim oleConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & myDB & ";User Id=admin;Password=;")

oleConn.Open()
Dim schemaTable As DataTable
Dim i As Integer
schemaTable = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Column s, _
New Object() {Nothing, Nothing, "tblTheTableToListColumns", Nothing})
For i = 0 To schemaTable.Columns.Count - 1
Debug.Print(schemaTable.Rows(i)!COLUMN_NAME.ToStri ng)
Next i
oleConn.Close()