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)?
Asked
Active
Viewed 3.5k times
17
-
What metadata are you in need of knowing about? – David-W-Fenton Nov 09 '09 at 21:08
-
At the very least, the description of the table (if one is saved) – Yaakov Ellis Nov 10 '09 at 04:25
-
possible duplicate of [How can I get a list of tables in an Access (Jet) database?](http://stackoverflow.com/questions/6152545/how-can-i-get-a-list-of-tables-in-an-access-jet-database) – Fionnuala Feb 09 '12 at 10:55
2 Answers
39
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());

Yaakov Ellis
- 40,752
- 27
- 129
- 174
0
Here's are some links:
- Displaying Tables of An Access Database Through C#
- Walkthrough: Editing an Access Database with ADO.NET
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:
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()

ParmesanCodice
- 5,017
- 1
- 23
- 20