如何使以编程方式创建的系统DSN显示为ODBC数据源?

时间:2015-01-06 18:12:01

标签: c# odbc dsn

所以,按照这个问题的接受答案的步骤:

How do I create an ODBC DSN entry using C#?

我已经创建了一个系统DSN。我可以使用regedit在注册表中看到它,并且我使用的所有值都设置正确,但是当我启动odbcad32.exe时,我看不到列出的DSN。在创建DSN时是否还有其他值,或者我没有做的其他步骤?在首先创建DSN时,我的代码在功能上与链接答案中的代码相同。您可以提供任何帮助。

编辑:这是我的实际代码:

public static void CreateDSN(ODBCInfo information)
{
    // SC - Try to get the specified driver for the ODBC.
    RegistryKey driverKey = null;

    try
    {
        driverKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + information.DriverName);
    }
    catch (Exception ex)
    {
        DataLayer.LogException(String.Format("Error while creating ODBC DSN: {0}", ex.ToString()));

        throw new Exception(String.Format("Error while creating ODBC DSN: {0}", ex.ToString()));
    }

    if (driverKey == null)
    {
        DataLayer.LogException(String.Format("Error while creating ODBC DSN: ODBC Registry Key for Driver {0} does not exist", information.DriverName));

        throw new Exception(String.Format("Error while creating ODBC DSN: ODBC Registry Key for Driver {0} does not exist", information.DriverName));
    }

    string driverPath = driverKey.GetValue("Driver").ToString();

    // SC - Add value to ODBC data sources.
    RegistryKey dataSourcesKey = null;

    try
    {
        dataSourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources");
    }
    catch (Exception ex)
    {
        DataLayer.LogException(String.Format("Error while creating ODBC DSN: {0}", ex.ToString()));

        throw new Exception(String.Format("Error while creating ODBC DSN: {0}", ex.ToString()));
    }

    if (dataSourcesKey == null)
    {
        DataLayer.LogException("Error while creating ODBC DSN: ODBC Registry Key for Datasources does not exist");

        throw new Exception("Error while creating ODBC DSN: ODBC Registry Key for Datasources does not exist");
    }

    // SC - Create new key in odbc.ini with DSN name, and other specified values.
    RegistryKey DSNKey = null;

    try
    {
        DSNKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + information.DSNName);
    }
    catch (Exception ex)
    {
        DataLayer.LogException(String.Format("Error while creating ODBC DSN: {0}", ex.ToString()));

       throw new Exception(String.Format("Error while creating ODBC DSN: {0}", ex.ToString()));
    }

    if (DSNKey == null)
    {
        DataLayer.LogException("Error while creating ODBC DSN: ODBC Registry Key for DSN was not created");

        throw new Exception("Error while creating ODBC DSN: ODBC Registry Key for DSN was not created");
    }

    DSNKey.SetValue("Database", information.Database);
    DSNKey.SetValue("Description", information.Description);
    DSNKey.SetValue("Driver", driverPath);
    DSNKey.SetValue("LastUser", Environment.UserName);
    DSNKey.SetValue("Server", information.Server);
    DSNKey.SetValue("Trusted_Connection", information.IsTrustedConnection ? "Yes" : "No");

    DataLayer.LogMessage("DBUpgrader successfully created ODBC DSN!");
}

ODBCInfo只是一个用于保存将在此处使用的数据的简单类。如果您觉得有必要也能看到这一点,我也可以发布该代码。

1 个答案:

答案 0 :(得分:0)

嗯,我意识到自己的错误。从ODBC数据源创建子键时,我实际上从未插入创建该键的行:

dataSourcesKey.SetValue(information.DSNName, information.DriverName);

添加该行使其按预期工作!不过,感谢您的建议和意见。