2011-07-12 21 views
2

我正在使用c#.net編寫應用程序。該應用程序依賴於PostgrSQL ODBC驅動程序,因此必須檢查驅動程序是否已安裝在用戶的計算機上。如果在我的應用程序自己的安裝過程中不需要安裝ODBC驅動程序。在設置過程中還必須配置DSN。如何在我的apllication安裝過程中使用C#安裝ODBC驅動程序?

因此,我的問題是,有沒有辦法在安裝過程中首先檢查驅動程序,如果不存在,是否可以將安裝程序設置爲自動安裝驅動程序並配置DSN?

我正在使用Microsoft Visual Studio 2010,但我看不到如何在發佈嚮導中包含其他安裝程序。我也努力在Google或者stackoverflow上找到任何有用的信息。

問候 彼得

回答

5

我找到了一個很好的例子,我在this website問題的第一部分。至於第二部分,「如何配置DSN」,我發現另一個code snippet只需要一些調整。

private const string ODBC_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\"; 
    private const string ODBCINST_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBCINST.INI\\"; 

    /// <summary> 
    /// Creates a new System-DSN entry with the specified values. If the DSN exists, the values are updated. 
    /// </summary> 
    /// <param name="dsnName">Name of the DSN for use by client applications</param> 
    /// <param name="description">Description of the DSN that appears in the ODBC control panel applet</param> 
    /// <param name="server">Network name or IP address of database server</param> 
    /// <param name="driverName">Name of the driver to use</param> 
    /// <param name="trustedConnection">True to use NT authentication, false to require applications to supply username/password in the connection string</param> 
    /// <param name="database">Name of the datbase to connect to</param> 
    public static void CreateDSN2(string dsnName, string description, string server, string driverName, bool trustedConnection, string database, string user, string password, string port) 
    { 
     // Lookup driver path from driver name 
     var driverKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + driverName); 
     if (driverKey == null) throw new Exception(string.Format("ODBC Registry key for driver '{0}' does not exist", driverName)); 
     string driverPath = driverKey.GetValue("Driver").ToString(); 

     // Add value to odbc data sources 
     var datasourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources"); 
     if (datasourcesKey == null) throw new Exception("ODBC Registry key for datasources does not exist"); 
     datasourcesKey.SetValue(dsnName, driverName); 

     // Create new key in odbc.ini with dsn name and add values 
     var dsnKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + dsnName); 
     //MessageBox.Show(dsnKey.ToString()); 
     if (dsnKey == null) throw new Exception("ODBC Registry key for DSN was not created"); 
     dsnKey.SetValue("Data Source", dsnName); 
     dsnKey.SetValue("Database", database); 
     dsnKey.SetValue("Description", description); 
     dsnKey.SetValue("Driver", driverPath); 
     dsnKey.SetValue("Server", server); 
     dsnKey.SetValue("User name", user); 
     dsnKey.SetValue("Password", password); 
     dsnKey.SetValue("Port", port); 
     dsnKey.SetValue("Trusted_Connection", trustedConnection ? "Yes" : "No"); 
    } 
相關問題