1.定义需要兼容的驱动
private readonly string[] connStrs = new string[] { @"Provider=Microsoft.ACE.OLEDB.16.0; Data Source =", @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=", @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" };
2.定义检测驱动的方法:
private bool IsOkConnStr(string connStr) { bool result = true; using (OleDbConnection conn = new OleDbConnection(connStr)) { try { conn.Open(); } catch (Exception ex) { // result = false; } finally { conn.Close(); } } return result; }
3.调用检测方法
private string GetConnStr(string pathdb) { string connStr = string.Empty; foreach (string con in connStrs) { connStr = con + Path.Combine(pathdb); if (IsOkConnStr(connStr)) { break; } } return connStr; }
4:获取适配驱动的访问连接字符串:
string connstring = GetConnStr("d:\myself.mdb");