2
I am performing the integration of my system with the SEFIP.
The database is in Interbase (.gbd).
I can connect 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 my system using Odbcconnection.
OdbcConnection OdbcConn = new OdbcConnection("DSN=Sefip");
OdbcConn.Open();
That way I do the consultations normally. The problem is that I will be using this integration on many clients, so it is impossible to create the ODBC connection manually at all. To avoid doing that, I’m trying to make the connection through the system. That answer shows a 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 are 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, this is the data on a working connection:
When creating by the system the rest of the data are empty, so:
My doubts are:
There is something wrong with the code to not fill in the connection data, even if the values are in the SetValue
?
Is there a Preview for Entity Framework or Dapper to perform this connection?
Is there any way to get the connection string from new OdbcConnection("DSN=Sefip");
so I can use in Fbconnection()?
For those who don’t know, this tutorial explains how to configure ODBC data source manually.
I know it’s stupid, but you gave it a
close
? Because on the piece of code you showed no such running it ondnskey
.– DH.
@Darkhyudra I only "create" the connection if it doesn’t exist. This check comes before opening the connection. After opening works normally. And yes, I gave
close
yes.– Randrade
@Andremesquita I did not get to test yet. I will test and return you
– Randrade
Use ADO DB and avoid creating DSN. Besides being simpler to do, it’s faster. Take a look at the site Connectionstrings.
– Andre Mesquita