0

Actually I'm trying to connect my winform data mining application to access database files through open dialogue box when application run . As I'm trying to connect to any unknown database file(mdb/accdb) so, I've no knowledge about its table names.but for connecting database to datagridview you have to provide sql query as command. My question is that , is there any way through which i can load all data(records) in any connected access database file without specifying/knowing tables names of a access db file.

thanks

1 Answers1

1

You can do is get or list table names first. As explained by Mr. Hayden on his blog http://davidhayden.com/blog/dave/archive/2006/10/01/GetListOfTablesInMicrosoftAccessUsingGetSchema.aspx

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

Retrieve List of Tables in MS Access File

Community
  • 1
  • 1
G. Pacete
  • 76
  • 1
  • 12
  • ok thanks but how I can use these tables in a sql query like SELECT * FROM _____ ; to load data from a all these table to datagridview – Umar Farooq Feb 01 '16 at 08:21
  • Table names are kept in "tableNames" list. All you just have to do is retrieve it. for (int i=0; i < tableNames.Count; i++) { SELECT * FROM tableNames[i].ToString(); ... ... ... } I guess you know you have to use 1 datagrid per one table of data if tables are not JOIN tables right? – G. Pacete Feb 01 '16 at 08:42
  • Nope actually I want to load all table''s data in single datagrid – Umar Farooq Feb 01 '16 at 09:49
  • Are those 2 or more tables have similar fields and/or number of fields inside? – G. Pacete Feb 02 '16 at 01:31