How to list all names of Access tables?

1

Hi, I'm wanting to display the names of the tables that are inside the Access file and play for a variable. How can I do this? Does the C # platform itself provide attributes so I can do this? I am using the System.Data.OleDb reference .

Below is the code I'm trying to get the tables, but it does not work.

 string path = "./info.mdb";
 OleDbConnection conn = new OleDbConnection(String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", path));    
 conn.Open();
 OleDbCommand odc = new OleDbCommand("SELECT * FROM " + tabela, conn);
 OleDbDataReader reader = odc.ExecuteReader();
 var tabelas = reader.GetSchemaTable();

Up in the table variable, I can not find the list of tables that is in the file info.mdb .

Can anyone help?

    
asked by anonymous 29.07.2018 / 23:12

1 answer

2

The GetSchema method that lists tables (and other MDB frameworks) that you should use is a Connection method, not a reader. The getSchemaTable of the reader is used to read the table schema defined in the query

Here is an example of how to read all the tables of an MDB

// 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());

Note:

Restrictions are an array that functions as a filter. If nothing is passed in it, all structures (tables, views, filters, forms, etc.) will be returned. Because OleDB is a generic provider (it serves both MDB and XLS, DBF, etc) the restrictions will depend on the base. I leave here the unofficial translation of the documentation:

  

The restrictionValues parameter can provide n depth values   that are specified in the collection of constraints for a collection   specific. To set values in a given constraint and not   define the values of the other constraints, you need to define the   previous constraints to null, and then put the value   appropriate for the constraint you want to specify a value for.

     

An example of this is the "Tables" collection. If the "Tables" collection has   three constraints (database, owner, and table name), and   you want to get only the tables associated with the owner   "Carl", you must pass the following values (at least): null,   "Carl." If a constraint value is not passed, the default values   are used for this restriction. This is the same mapping as pass   null, which is different from passing an empty string   for the value of the parameter. In this case, the empty string   ("") is considered the value for the specified parameter.

    
30.07.2018 / 03:44