0

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.

Community
  • 1
  • 1
Sean Cogan
  • 2,516
  • 2
  • 27
  • 42
  • `Following the steps of the accepted answer` Good for you.. now display to use the actual code that you are using perhaps you missed a step..? have you debugged your own solution to try and pinpoint where your disconnect is...? showing no code is like asking a blind man to guide you around at night also is there a specific reason you have to use ODBC and DNS Entry..? just curious perhaps you could refactor the code you have and make it more efficient using today's standards / best practices.. – MethodMan Jan 06 '15 at 18:23
  • Did you read the Comments under the accepted answer as well this is why I have asked if you could show us exactly what you are working with.. – MethodMan Jan 06 '15 at 18:26
  • I did debug the code and from what I can tell, it works as it should. I also did what the comments under the accept answer said and got no results from that. I'll update the question with my actual code in a second... – Sean Cogan Jan 06 '15 at 18:32
  • can you explain Sean, why you are using ODBC perhaps there are better ways to accomplish what you are trying to do.. – MethodMan Jan 06 '15 at 18:33
  • I am using ODBC because this code is part of a solution that will be used to upgrade databases that rely on an ODBC connection. – Sean Cogan Jan 06 '15 at 18:37
  • Are you seeing your connection when you use the odbcad32.exe located in C:\Windows\SysWOW64 ? – Paul-Jan Jan 06 '15 at 19:28
  • @Paul-Jan, no, that's what I'm trying to accomplish. I can see it when I look in the registry, though. – Sean Cogan Jan 06 '15 at 20:13

1 Answers1

0

Well, I realized my own mistake. When creating the subkey off of ODBC Data Sources, I never actually put in the line that creates that key:

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

Adding that line made it work as intended! Thanks for the suggestions and comments, though.

Sean Cogan
  • 2,516
  • 2
  • 27
  • 42