Create ODBC Connection

2

I'm integrating my system with the database. SEFIP .
The database is in Interbase (.gbd).
I can make the connection to the database via Microsoft Access normally. I created an ODBC connection manually (Run: odbcad32 or Administrative Tools -> ODBC Data Sources) and I can connect to my system using the OdbcConnection .

OdbcConnection OdbcConn = new OdbcConnection("DSN=Sefip");
OdbcConn.Open();

In this way I perform the queries normally. The problem is that I will use this integration on many clients, so it is impractical to create the ODBC connection manually at all. To avoid doing this, I'm trying to create the connection through the system. This answer shows one way to do this, but the user and password are not passed to the connection.

This part was to fill in all the data. Although the data is in their respective variables, it is not saved in the DNS.

 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("Trusted_Connection", trustedConnection ? "Yes" : "No");

The complete code can be seen below:

 private const string ODBC_INI_REG_PATH = "SOFTWARE\ODBC\ODBC.INI\";
        private const string ODBCINST_INI_REG_PATH = "SOFTWARE\ODBC\ODBCINST.INI\";

        /// <summary>
        /// Creates a new DSN entry with the specified values. If the DSN exists, the values are updated.
        /// </summary>
        /// <param name="dsnName">Name of the DSN for use by client applications</param>
        /// <param name="description">Description of the DSN that appears in the ODBC control panel applet</param>
        /// <param name="server">Network name or IP address of database server</param>
        /// <param name="driverName">Name of the driver to use</param>
        /// <param name="trustedConnection">True to use NT authentication, false to require applications to supply username/password in the connection string</param>
        /// <param name="database">Name of the datbase to connect to</param>
        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("Trusted_Connection", trustedConnection ? "Yes" : "No");
        }

When creating manually, these are the data in a working connection:

Whencreatedbythesystemtherestofthedataisempty,likethis:

Mydoubtsare:Istheresomethingwrongwiththecodetonotpopulatetheconnectiondata,eventhevaluesbeingSetValue?IsthereanyproviderforEntityFrameworkorDappertomakethisconnection?
IsthereanywaytogettheconnectionstringofnewOdbcConnection("DSN=Sefip"); so I can use it in FbConnection () ?

For those who do not know, this tutorial explains how to set up source ODBC data manually.

    
asked by anonymous 21.01.2016 / 17:39

1 answer

1

The solution was to change the CreateDNS method to pass the remaining parameters, including the Interbase driver. The method looks like this:

 public static void CreateDSN(string dsnName, string description, string server, string driverName,
            bool trustedConnection, string database, string user, string password, string client)
        {
            // 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("DBName", database);
            dsnKey.SetValue("Description", description);
            dsnKey.SetValue("Driver", driverPath);
            dsnKey.SetValue("LastUser", Environment.UserName);
            dsnKey.SetValue("Server", server);
            dsnKey.SetValue("User", user);
            dsnKey.SetValue("Password", password);
            dsnKey.SetValue("Client", client);
            dsnKey.SetValue("Trusted_Connection", trustedConnection ? "Yes" : "No");
        }
    
22.01.2016 / 16:43