Search code examples
c#databaseoledb

Getting MsAccess table names


I am looking to get all the tables given an MS-Access file. The following is what I do:

public override List<string> GetTables()
{
    using (OleDbConnection con = new OleDbConnection(Path))
    {
        con.Open();
        DataTable schema = con.GetSchema("Columns");
        List<string> tables= new List<string>();
        foreach (DataRow row in schema.Rows)
        {
            tables.Add(row.Field<string>("TABLE_NAME"));
        }
        return tables;
    }
}

However, although all the table names are returned, it seems each one is returned 10 times. Why is it doing that?


Solution

  • I think you need to change your con.GetSchema call to get Tables not Columns - e.g.

    DataTable schema = con.GetSchema("Tables");
    

    It looks like you are getting a list of all the columns in the database and then only using the table name fields in the results, so it will appear multiple times.