Retrieve List of Tables in MS Access File

32,943

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());
Share:
32,943
Thiago Silva
Author by

Thiago Silva

Husband, father, learner, and programmer. Trying to live life as it should be lived. Developer @ Stack Overflow since 10/2013. Currently Staff Software Engineer and Tech Lead on the Public Platform team, and Community Advocate. Email me at [email protected]. Former Team Lead &amp; Principal Web Dev, Internal Dev Team through 4/2019.

Updated on November 09, 2020

Comments

  • Thiago Silva
    Thiago Silva over 3 years

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

  • MindRoasterMir
    MindRoasterMir almost 4 years
    Thanks. It worked for me. C# WPF Visual Studio 2019