如何使以编程方式创建的系统 DSN 显示为 ODBC 数据源?
How do I make a programmatically-created System DSN show up as an ODBC Data Source?
所以,按照这个问题的公认答案的步骤:
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只是一个简单的class我用来保存这里要用到的数据。如果你也觉得有必要看,我也可以 post 那个代码。
好吧,我意识到自己的错误。当从 ODBC 数据源创建子键时,我实际上从未输入创建该键的行:
dataSourcesKey.SetValue(information.DSNName, information.DriverName);
添加该行使其按预期工作!不过还是感谢您的建议和意见。
所以,按照这个问题的公认答案的步骤:
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只是一个简单的class我用来保存这里要用到的数据。如果你也觉得有必要看,我也可以 post 那个代码。
好吧,我意识到自己的错误。当从 ODBC 数据源创建子键时,我实际上从未输入创建该键的行:
dataSourcesKey.SetValue(information.DSNName, information.DriverName);
添加该行使其按预期工作!不过还是感谢您的建议和意见。