检索表名单在MS Access文件 [英] Retrieve List of Tables in MS Access File

查看:107
本文介绍了检索表名单在MS Access文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我可以打开在C#中的MS Access文件的连接,我怎么能检索中存在的Access数据库的不同表的列表(如果可能的话,与表相关联的所有元数据)?

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)?

推荐答案

我刚刚发现下面从<解决方案href="http://davidhayden.com/blog/dave/archive/2006/10/01/GetListOfTablesInMicrosoftAccessUsingGetSchema.aspx">David海登

// 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());

这篇关于检索表名单在MS Access文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆