Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在应用程序安装过程中,如何使用C#安装ODBC驱动程序?_C#_.net_Odbc_Driver_Dsn - Fatal编程技术网

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

在应用程序安装过程中,如何使用C#安装ODBC驱动程序?,c#,.net,odbc,driver,dsn,C#,.net,Odbc,Driver,Dsn,我正在使用c#net制作一个应用程序。应用程序依赖于PostgrSQL ODBC驱动程序,因此必须检查用户计算机上是否已安装该驱动程序。如果没有,则必须在我的应用程序自己的安装过程中安装ODBC驱动程序。DSN还必须在设置过程中进行配置 因此,我的问题是,是否有办法在安装过程中首先检查驱动程序,如果没有,是否可以将安装设置为自动安装驱动程序并配置DSN 我正在使用Microsoft Visual Studio 2010,但看不到如何在发布向导中包含其他设置。我也在努力寻找关于谷歌或stackov

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

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

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

问候
彼得

请看

我找到了一个很好的例子来回答我问题的第一部分

至于第二部分,“如何配置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");
    }
private const string ODBC_INI_REG_PATH=“SOFTWARE\\ODBC\\ODBC.INI\\”;
私有常量字符串ODBCINST_INI_REG_PATH=“SOFTWARE\\ODBC\\ODBCINST.INI\\”;
/// 
///使用指定的值创建新的系统DSN条目。如果DSN存在,则更新值。
/// 
///供客户端应用程序使用的DSN的名称
///ODBC控制面板小程序中显示的DSN的说明
///数据库服务器的网络名称或IP地址
///要使用的驱动程序的名称
///True表示使用NT身份验证,false表示要求应用程序在连接字符串中提供用户名/密码
///要连接到的数据库的名称
public static void CreateDSN2(字符串dsnName、字符串描述、字符串服务器、字符串驱动器名、bool trustedConnection、字符串数据库、字符串用户、字符串密码、字符串端口)
{
//从驱动程序名称查找驱动程序路径
var driverKey=Registry.LocalMachine.CreateSubKey(ODBCINST\u INI\u REG\u PATH+driverName);
如果(driverKey==null)抛出新异常(string.Format(“驱动程序“{0}”的ODBC注册表项不存在”,driverName));
string driverPath=driverKey.GetValue(“驱动程序”).ToString();
//为odbc数据源添加值
var datasourcesKey=Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH+“ODBC数据源”);
if(datasourcesKey==null)抛出新异常(“datasources的ODBC注册表项不存在”);
datasourcesKey.SetValue(dsnName、driverName);
//使用dsn名称在odbc.ini中创建新键并添加值
var dsnKey=Registry.LocalMachine.CreateSubKey(ODBC\u INI\u REG\u PATH+dsnName);
//Show(dsnKey.ToString());
if(dsnKey==null)抛出新异常(“未创建DSN的ODBC注册表项”);
设置值(“数据源”,dsnName);
设置值(“数据库”,数据库);
dsnKey.SetValue(“说明”,说明);
dsnKey.SetValue(“驱动程序”,driverPath);
设置值(“服务器”,服务器);
dsnKey.SetValue(“用户名”,用户);
dsnKey.SetValue(“密码”,Password);
dsnKey.SetValue(“端口”,端口);
dsnKey.SetValue(“受信任的连接”,受信任的连接?“是”:“否”);
}