如何在应用程序设置过程中使用 C# 安装 ODBC 驱动程序?

发布于 2024-11-20 00:44:16 字数 327 浏览 5 评论 0原文

我正在使用 c#.net 制作一个应用程序。该应用程序依赖于 PostgrSQL ODBC 驱动程序,因此必须检查用户计算机上是否已安装该驱动程序。如果没有,则必须在我的应用程序自己的安装过程中安装 ODBC 驱动程序。 DSN 还必须在安装过程中进行配置。

因此,我的问题是,有没有办法在安装过程中首先检查驱动程序,如果不存在,是否可以将安装程序设置为自动安装驱动程序并配置 DSN?

我正在使用 Microsoft Visual Studio 2010,但我看不到如何在发布向导中包含其他设置。我也在努力在 Google 或 stackoverflow 上找到任何有用的信息。

问候 彼得

I am making an application using c#.net. The application relies on a PostgrSQL ODBC driver and therefore has to check if the driver has already been installed on the user’s computer. If not the ODBC driver has to be installed during my applications own installation process. The DSN also has to be configured during the setup.

My question is therefore, is there a way to first check for a driver during setup and, if it’s not there, can the setup be set to automatically install a driver and configure the DSN?

I’m using Microsoft Visual Studio 2010 but I can’t see how to include another setup in the publishing wizard. I am also struggling to find any useful information on Google or stackoverflow.

Regards
Peter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑丑阿 2024-11-27 00:44:16

我在此网站上找到了一个很好的示例来解决我的问题的第一部分。

至于第二部分“如何配置DSN”,我找到了另一个代码片段只需要一些调整。

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

I found a good example for the first part of my question on this website.

As for the second part, "how to configure the DSN", I found another code snippet which only needed some tweaking.

    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");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文