如何使用.net确定Windows机器上的ACE或JET?

wil*_*lem 10 .net c# jet

如何确定(对于运行windows xp/vista/7的机器)是否安装了ACE或JET,因此我可以使用适当的连接字符串连接到访问数据库.

Rod*_*ahn 8

您可以检查一个注册表项.它在HKCR\Microsoft.ACE.OLEDB.12.0.您可以使用该RegistryKey课程阅读它.


Der*_*son 5

同意,注册表不是最好的方法(32位和64位版本以及用户权限等)但是,原始问题陈述"使用适当的连接字符串" - 这是运行时问题,而不是编译时问题.解决方案可能是使用OleDbEnumerator.

下面是一个检查32位版本的Microsoft Access(ACE 12.0)的示例.对于x86应用程序:

using System.Data;
using System.Data.OleDb;

    static bool AceOleDb12Present()
    {
        OleDbEnumerator enumerator = new OleDbEnumerator();
        DataTable table = enumerator.GetElements();
        bool bNameFound = false;
        bool bCLSIDFound = false;

        foreach (DataRow row in table.Rows)
        {
            foreach (DataColumn col in table.Columns)
            {
                if ((col.ColumnName.Contains("SOURCES_NAME")) && (row[col].ToString().Contains("Microsoft.ACE.OLEDB.12.0")))
                    bNameFound = true;
                if ((col.ColumnName.Contains("SOURCES_CLSID")) && (row[col].ToString().Contains("{3BE786A0-0366-4F5C-9434-25CF162E475E}")))
                    bCLSIDFound = true;
            }
        }
        if (bNameFound && bCLSIDFound)
            return true;
        else
            return false;
    }
Run Code Online (Sandbox Code Playgroud)