如何从c#以编程方式正确安装Firebird ODBC驱动程序?

时间:2016-03-22 13:14:10

标签: c# windows firebird2.5

我们目前在ERP中附带了一个简单的实用程序,可以通过编程方式安装Firebird Server 2.5和Firebird ODBC驱动程序。

  • 我通过从该实用程序执行一些.bat文件来安装服务器,它在端口3060上运行良好。
  • 我通过将odbc密钥添加到注册表然后将gds32.dll和odbcfb.dll文件复制到system32文件夹来安装ODBC。

我们一直使用此实用程序已超过一年且客户端安装正常,但有时旧版本的Firebird Server或Interbase使用system32文件夹上的旧版gds32.dll服务器和我不能简单地更新gds32.dll,因为它会阻止任何其他使用它的软件。

这是驱动程序安装代码:

class OdbcDriverHelper
{
    private static string ODBCINST_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBCINST.INI\\";
    private static string ODBCINST_INI_REG_PATH_DRIVERS = "SOFTWARE\\ODBC\\ODBCINST.INI\\ODBC Drivers\\";
    private static string ODBCINST_INI_REG_PATH_6432 = "SOFTWARE\\Wow6432Node\\ODBC\\ODBCINST.INI\\";

    public static void AddEntry(string nomeDriver, string caminhoDriver)
    {
        var driverKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + nomeDriver);
        if (driverKey == null) throw new Exception(string.Format("ODBC Registry key for driver '{0}' does not exist", nomeDriver));

        driverKey.SetValue("APILevel", 1);
        driverKey.SetValue("ConnectFunctions", "YYY");
        driverKey.SetValue("Driver", caminhoDriver);
        driverKey.SetValue("DriverODBCVer", 03.51);
        driverKey.SetValue("FileExtns", "*.fdb,*.gdb");
        driverKey.SetValue("FileUsage", 0);
        driverKey.SetValue("Setup", caminhoDriver);
        driverKey.SetValue("SQLLevel", 1);
        driverKey.SetValue("UsageCount", 0x00000001);

        if (SystemInfo.is64BitOperatingSystem)
        {
            driverKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH_6432 + nomeDriver);
            if (driverKey == null) throw new Exception(string.Format("ODBC Registry key for driver on WIN6432NODE '{0}' does not exist", nomeDriver));

            driverKey.SetValue("APILevel", 1);
            driverKey.SetValue("ConnectFunctions", "YYY");
            driverKey.SetValue("Driver", caminhoDriver);
            driverKey.SetValue("DriverODBCVer", 03.51);
            driverKey.SetValue("FileExtns", "*.fdb,*.gdb");
            driverKey.SetValue("FileUsage", 0);
            driverKey.SetValue("Setup", caminhoDriver);
            driverKey.SetValue("SQLLevel", 1);
            driverKey.SetValue("UsageCount", 0x00000001);
        }
    }

    public static void InstallDriver()
    {
        if (!File.Exists(Constants.CAM_DRIVER_32))
                File.Copy("Firebird_32\\odbc\\OdbcFb.dll", Constants.CAM_DRIVER_32, true);

        if (!File.Exists(Constants.CAM_GDS32_32))
                File.Copy("Firebird_32\\odbc\\GDS32.dll", Constants.CAM_GDS32_32, true);

        AddEntry(Constants.ODBC_DRIVER_NAME, Constants.CAM_DRIVER_32);
    }
}

注意:这不是原始代码,还有一些变量我切换到硬编码字符串,还有一些尝试捕获后备,我不包括只是为了让它更干净。

有没有其他正确的方法可以做到这一点,而不必担心这些类型的冲突,你将如何更有效地进行这种程序化安装?

0 个答案:

没有答案