1.在 x64 環境下才能達到
2.連接字串中的Provider 要用 Microsoft.ACE.OLEDB.12.0,例:
<add name="connAccess" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Access檔案路徑(包含副檔名);Persist Security Info=False"/>
/// <summary>
/// 取得Access的表格名稱清單
/// </summary>
/// <returns></returns>
public static KeyValuePair<int, string>[] GetTableNames()
{
List<string> listTableName = new List<string>();
try
{
if (filePath.Length == 0)
throw new Exception("未提供檔案(*.mdb)路徑.");
ADODB.Connection connection = new ADODB.Connection();
ADOX.Catalog tableLog = new ADOX.Catalog();
try
{
connection.Open(defaultConnectionString.Replace("#FilePath#", filePath));
tableLog.ActiveConnection = connection;
for (int i = 0; i < tableLog.Tables.Count; i++)
{
if (((dynamic)tableLog.Tables[i]).Type == "TABLE")
{
listTableName.Add(tableLog.Tables[i].Name);
}
}
}
finally
{
if (connection != null)
connection.Close();
}
}
catch (Exception ex)
{
throw ex;
}
KeyValuePair<int,string>[] ret = new KeyValuePair<int,string>[listTableName.Count];
for (int i = 0; i < listTableName.Count; i++)
{
ret[i] = new KeyValuePair<int, string>(i, listTableName[i]);
}
return ret;
}
沒有留言:
張貼留言