Getting a list of tables in an Access database (C#)
Posting this code so that I can remember it/ and in case anyone else finds it useful.
First open the database
[color="blue"]string [/color]constr = [color="#8b0000"]@"Provider=Microsoft.Jet.OLEDB.4.0;Data source=database.mdb"[/color]; [color="teal"]OleDbConnection [/color]con = [color="blue"]new [/color][color="teal"]OleDbConnection[/color](constr); con.Open();
con.GetSchema() will return a list of collections in the database.
CollectionName NumberOfRestrictions NumberOfIdentifierParts --------------------- -------------------- ----------------------- MetaDataCollections 0 0 DataSourceInformation 0 0 DataTypes 0 0 Restrictions 0 0 ReservedWords 0 0 Columns 4 4 Indexes 5 4 Procedures 4 3 Tables 4 3 Views 3 3
The Tables collection contains the information we want, so call con.GetSchema("Tables").
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE TABLE_GUID DESCRIPTION TABLE_PROPID DATE_CREATED DATE_MODIFIED
-------------- ------------- --------------------------- ----------- ---------- ----------- ------------ ---------------- ---------------
clschk TABLE 29/12/2010 09:20 29/12/2010 09:20
coursedesc TABLE 29/12/2010 09:20 10/01/2011 09:00
MSysAccessStorage ACCESS TABLE 29/12/2010 09:30 29/12/2010 09:30
MSysACEs SYSTEM TABLE 29/12/2010 09:20 29/12/2010 09:20
MSysNavPaneGroupCategories ACCESS TABLE 29/12/2010 09:30 29/12/2010 09:30
MSysNavPaneGroups ACCESS TABLE 29/12/2010 09:30 29/12/2010 09:30
MSysNavPaneGroupToObjects ACCESS TABLE 29/12/2010 09:30 29/12/2010 09:30
MSysNavPaneObjectIDs ACCESS TABLE 29/12/2010 09:30 29/12/2010 09:30
MSysObjects SYSTEM TABLE 29/12/2010 09:20 29/12/2010 09:20
MSysQueries SYSTEM TABLE 29/12/2010 09:20 29/12/2010 09:20
MSysRelationships SYSTEM TABLE 29/12/2010 09:20 29/12/2010 09:20
sessioninfo TABLE 29/12/2010 09:20 29/12/2010 09:20
y11_subjectpred TABLE 29/12/2010 09:20 29/12/2010 11:50
y11_tutorltnopro TABLE 29/12/2010 09:20 29/12/2010 09:20
All of the user-created tables have a TABLE_TYPE of "TABLE".
The following code puts them into a list:
[color="teal"]List[/color]<[color="blue"]string[/color]> tables = [color="blue"]new [/color][color="teal"]List[/color]<[color="blue"]string[/color]>(); [color="blue"]foreach [/color]([color="teal"]DataRow [/color]r in con.GetSchema([color="#8b0000"]"Tables"[/color]).Select([color="#8b0000"]"TABLE_TYPE = 'TABLE'"[/color])) tables.Add(r[[color="#8b0000"]"TABLE_NAME"[/color]].ToString());

1 Comment
Recommended Comments
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now