如果我可以在C#中打开到MS Access文件的连接,如何检索Access DB中存在的不同表的列表(如果可能,检索与这些表关联的元数据)?
发布于 2009-11-09 09:19:43
我刚刚从David Hayden找到了以下解决方案
// 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());
发布于 2009-11-09 09:17:04
以下是一些链接:
这是一个获取访问表的所有列的VB.NET片段,我知道它不完全是你想要的,但在列出所有表时,它是一个类似的原生苹果:
Dim oleConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & myDB & ";User Id=admin;Password=;")
oleConn.Open()
Dim schemaTable As DataTable
Dim i As Integer
schemaTable = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Column s, _
New Object() {Nothing, Nothing, "tblTheTableToListColumns", Nothing})
For i = 0 To schemaTable.Columns.Count - 1
Debug.Print(schemaTable.Rows(i)!COLUMN_NAME.ToStri ng)
Next i
oleConn.Close()
https://stackoverflow.com/questions/1699897
复制相似问题