How can I get a list of tables in an Access (Jet) database?

前端 未结 4 1940
不思量自难忘°
不思量自难忘° 2020-12-16 17:18

I need to see if a table exists in an Access database used by my c# program. Is know there are SQL commands for other databases that will return a list of tables. Is there s

相关标签:
4条回答
  • 2020-12-16 17:47

    Full code : Get List of Tables in an Access Database - ADO.NET Tutorials

    // 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);
    }
    
    // Add list of table names to listBox
    for (int i=0; i < userTables.Rows.Count; i++)
        listBox1.Items.Add(userTables.Rows[i][2].ToString())
    

    here is answer for you : http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/d2eaf851-fc06-49a1-b7bd-bca76669783e

    0 讨论(0)
  • 2020-12-16 17:59

    Try the GetSchema()

        connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\access.mdb";    
    
        connection.Open();
    
        DataTable userTables = connection.GetSchema("Tables");
    
    0 讨论(0)
  • 2020-12-16 18:03

    that one worked for me

    using (OleDbConnection con = new OleDbConnection(connectionString))
                {
                    con.Open();
                    DataTable dt = con.GetSchema("Tables");
                    var selectNames = dt.Rows.Cast<DataRow>().Where(c => !c["TABLE_NAME"].ToString().Contains("MSys")).ToArray();
                    foreach (var item in selectNames)
                    {
                         // add names to comboBox
                        comboBox1.Items.Add(item["TABLE_NAME"]);
                    }
                }
    
    0 讨论(0)
  • 2020-12-16 18:06

    Something like this should do the trick. The clause Type = 1 specifies tables. Note that this will also include the system tables in the result set (they start with the prefix "MSys".

    SELECT Name FROM MSysObjects WHERE Type = 1
    
    0 讨论(0)
提交回复
热议问题