2011-05-27 43 views

回答

11

嘗試的getSchema()

connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\access.mdb";  

    connection.Open(); 

    DataTable userTables = connection.GetSchema("Tables"); 
+0

這就是我想要的所有很多大花哨的答案!謝謝!! – CindyH 2014-02-28 17:06:42

7

全碼: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()) 

這裏是爲你解答:http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/d2eaf851-fc06-49a1-b7bd-bca76669783e

1

像這樣的東西應該做的伎倆。條款Type = 1指定了表格。請注意,這也將包括在結果集中的系統表(它們以前綴「MSys的」

SELECT Name FROM MSysObjects WHERE Type = 1 
+0

只是想它,我得到:'記錄(s)不能被讀取;沒有'MSysObjects'的讀取權限 – hawbsl 2011-05-27 13:13:41

+0

@hawbsl聽起來你可能需要在Access數據庫中修改權限工具菜單 - >安全性 - >用戶和組權限分配'讀取數據'對MSysObjects表上的Admin用戶的權限 – 2011-05-27 13:34:41

+0

更改這樣的權限可能會違反已故意設置的用戶級安全性。他的解決方案不是更改底層對象的權限(這會使其對任何人都開放),而是使用具有所需權限的用戶名/密碼。 – 2011-05-28 21:19:41

相關問題