如何从Access数据库获取所有表和列的列表 [英] How to get list of all tables and columns from Access database

查看:274
本文介绍了如何从Access数据库获取所有表和列的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


i希望获取特定Access数据库中所有表及其列的列表

i尝试

 SELECT * FROM MSysObjects 



但没有检索到任何内容

解决方案

您可以使用MS Access Provider工厂获取表列表。从网上获得解决方案。

  //   Microsoft Access提供者工厂 
DbProviderFactory factory = DbProviderFactories.GetFactory( System.Data.OleDb);

DataTable userTables = null ;
使用(DbConnection connection = factory.CreateConnection()){
// c:\\\\test.mdb
connection.ConnectionString = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = c:\\test\\test.mdb;
// 我们只需要用户表,而不是系统表
string [] restrictions = new string [ 4 ];
限制[ 3 ] = ;

connection.Open();

// 获取用户表列表
userTables = connection .GetSchema( ,限制条件);
}

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


Hi i want to get a list of all tables and their columns from a specific Access database
i tried

SELECT * FROM MSysObjects


but nothing retrieved

解决方案

You can get the list of tables by using MS Access Provider factory. Got the solution from web.

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


这篇关于如何从Access数据库获取所有表和列的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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