得到Access数据库中所有表名

时间:2021-09-11 13:54:52

public static List<string> GetShemaTables(string db)
        {           
            string path = Path.Combine(GlobalSettings.UnZipPath, db+".mdb");
            string ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", path);
            //获取数据表
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = ConnectionString;
            try
            {  
                conn.Open();
                DataTable shemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                int n = shemaTable.Rows.Count;
                List<string> tabs = new List<string>();
                int m = shemaTable.Columns.IndexOf("TABLE_NAME");
                for (int i = 0; i < n; i++)
                {
                    DataRow m_DataRow = shemaTable.Rows[i];
                    tabs.Add(m_DataRow.ItemArray.GetValue(m).ToString());
                }
                return tabs;
            }
            catch (OleDbException ex)
            {
                MessageBox.Show("指定的限制集无效:\n" + ex.Message);
                return null;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }