Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve List of Tables in MS Access File

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

like image 784
Yaakov Ellis Avatar asked Nov 09 '09 09:11

Yaakov Ellis


People also ask

How do I get a list of tables in Access?

Add the table called [MSysObjects] and select the fields called [Name] and [Type]. The criteria of [Type] = 5 will get your query names, and a criteria of [Type] = 1 with a [Flags] = 0 will get your table names.

How do I see all tables in Access database?

On the Home tab, in the Find group, click Find. The Find and Replace dialog box appears, with the Find tab selected. In the Find What box, type the value for which you want to search. To change the field that you want to search or to search the entire underlying table, click the appropriate option in the Look In list.


1 Answers

I just found the following solution from 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());
like image 186
Yaakov Ellis Avatar answered Oct 26 '22 02:10

Yaakov Ellis