当前位置:  开发笔记 > 编程语言 > 正文

如何使用C#创建ODBC DSN条目?

如何解决《如何使用C#创建ODBCDSN条目?》经验,为你挑选了3个好方法。

我正在研究具有C++扩展存储过程的遗留应用程序.此xsproc使用ODBC连接到数据库,这意味着它需要配置DSN.

我正在更新安装程序(使用Visual Studio 2008安装项目创建),并希望有一个可以创建ODBC DSN条目的自定义操作,但我很难在Google上找到有用的信息.

有人可以帮忙吗?



1> Neil Barnwel..:

我最终通过操纵注册表来解决这个问题.我已经创建了一个包含功能的类,我在这里包含的内容如下:

///
/// Class to assist with creation and removal of ODBC DSN entries
///
public static class ODBCManager
{
    private const string ODBC_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\";
    private const string ODBCINST_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBCINST.INI\\";

    /// 
    /// Creates a new DSN entry with the specified values. If the DSN exists, the values are updated.
    /// 
    /// Name of the DSN for use by client applications
    /// Description of the DSN that appears in the ODBC control panel applet
    /// Network name or IP address of database server
    /// Name of the driver to use
    /// True to use NT authentication, false to require applications to supply username/password in the connection string
    /// Name of the datbase to connect to
    public static void CreateDSN(string dsnName, string description, string server, string driverName, bool trustedConnection, string database)
    {
        // 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);
        if (dsnKey == null) throw new Exception("ODBC Registry key for DSN was not created");
        dsnKey.SetValue("Database", database);
        dsnKey.SetValue("Description", description);
        dsnKey.SetValue("Driver", driverPath);
        dsnKey.SetValue("LastUser", Environment.UserName);
        dsnKey.SetValue("Server", server);
        dsnKey.SetValue("Database", database);
        dsnKey.SetValue("Trusted_Connection", trustedConnection ? "Yes" : "No");
    }

    /// 
    /// Removes a DSN entry
    /// 
    /// Name of the DSN to remove.
    public static void RemoveDSN(string dsnName)
    {
        // Remove DSN key
        Registry.LocalMachine.DeleteSubKeyTree(ODBC_INI_REG_PATH + dsnName);

        // Remove DSN name from values list in ODBC Data Sources key
        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.DeleteValue(dsnName);
    }

    ///
    /// Checks the registry to see if a DSN exists with the specified name
    ///
    ///
    ///
    public static bool DSNExists(string dsnName)
    {
        var driversKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + "ODBC Drivers");
        if (driversKey == null) throw new Exception("ODBC Registry key for drivers does not exist");

        return driversKey.GetValue(dsnName) != null;
    }

    ///
    /// Returns an array of driver names installed on the system
    ///
    ///
    public static string[] GetInstalledDrivers()
    {
        var driversKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + "ODBC Drivers");
        if (driversKey == null) throw new Exception("ODBC Registry key for drivers does not exist");

        var driverNames = driversKey.GetValueNames();

        var ret = new List();

        foreach (var driverName in driverNames)
        {
            if (driverName != "(Default)")
            {
                ret.Add(driverName);
            }
        }

        return ret.ToArray();
    }
}



2> ljs..:

除了chrfalch的帖子之外,这里有一些用于更新DSN的示例代码(我知道OP要求创建,但是这个代码很容易翻译成你需要做的任何事情)使用API​​调用而不是直接通过注册表(使用来自pinvoke.net页面的信息): -

[DllImport("ODBCCP32.DLL", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool SQLConfigDataSourceW(UInt32 hwndParent, RequestFlags fRequest, string lpszDriver, string lpszAttributes);

enum RequestFlags : int
{
    ODBC_ADD_DSN = 1,
    ODBC_CONFIG_DSN = 2,
    ODBC_REMOVE_DSN = 3,
    ODBC_ADD_SYS_DSN = 4,
    ODBC_CONFIG_SYS_DSN = 5,
    ODBC_REMOVE_SYS_DSN = 6,
    ODBC_REMOVE_DEFAULT_DSN = 7
}

bool UpdateDsnServer(string name, string server)
{
    var flag = RequestFlags.ODBC_CONFIG_SYS_DSN;
    string dsnNameLine = "DSN=" + name;
    string serverLine = "Server=" + server;

    string configString = new[] { dsnNameLine, serverLine }.Aggregate("", (str, line) => str + line + "\0");

    return SQLConfigDataSourceW(0, flag, "SQL Server", configString);
}



3> chrfalch..:

有一个用于执行此类操作的API.使用API​​还可确保您的应用程序与新版本的Windows保持兼容.API可以在这里找到:

http://msdn.microsoft.com/en-us/library/ms716476(VS.85).aspx

可以在PInvoke.net上找到在c#中调用此函数.

推荐阅读
路人甲
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有