Access 数据库多驱动兼容,目标机器不确定安装的驱动版本

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");

 



上一篇:Dubbo-go Consumer调用Dubbo-java Provider


下一篇:C# RSA的加解密与签名验证