So, following the steps of the accepted answer to this question:
How do I create an ODBC DSN entry using C#?
I've created a System DSN. I can see it in the registry using regedit, and all of the values I used are set properly, but when I launch odbcad32.exe, I cannot see the DSN I created listed. Is there another value I have to set when creating the DSN, or some other step I did not do? When creating the DSN in the first place, my code is functionally identical to the code in the linked answer. Any help that you can provide is appreciated.
EDIT: Here is my actual code:
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 is just a simple class I used to hold the data that will be used here. If you feel the need to see that, too, I can post that code as well.