Edit / Create ODBC Connection from Code

I am working against an Informix database with C # ASP.NET 4.0 through an ODBC connection. This database often changes, so I read which DBs that are installed from the sysmaster table.

Depending on the choice that my user makes, I need to configure the ODBC connection to the selected database or change the current connection in order to change it from the Systmaster database to the selected one.

Has anyone figured out how to do this? I suspect you need to establish a temporary ODBC connection. Also in Visual Studio, in the properties of my ODBC connection, I have a connection string that looks like this:

Dsn=Informix;uid=xxxxx;database=sysmaster;host=10.10.10.10;srvr=testdb1;serv=3000;pro=onsoctcp;cloc=en_US.819;dloc=en_US.819;vmb=0;curb=0;scur=0;icur=0;oac=1;optofc=0;rkc=0;odtyp=0;ddfp=0;dnl=0;rcwc=0 

I looked at the library for direct connection without ODBC to informix, but with success.

Thanks Stefan

+4
source share
2 answers

I developed a working solution that is pretty nice. I did not realize that .NET had full support to do this from code without changing ODBC settings.

  const string sConnString = "Driver=Informix;uid=user;pwd=password;database=x10stg01_1312;host=10.10.10.10;srvr=testdb1;serv=3000;pro=onsoctcp;cloc=en_US.819;dloc=en_US.819;vmb=0;curb=0;scur=0;icur=0;oac=1;optofc=0;rkc=0;odtyp=0;ddfp=0;dnl=0;rcwc=0"; var oOdbcConnection = new System.Data.Odbc.OdbcConnection(sConnString); string queryString = "SELECT * FROM tevoc WHERE ev_oc_id=6599098"; OdbcCommand command = new OdbcCommand(queryString); command.Connection = oOdbcConnection; oOdbcConnection.Open(); OdbcDataReader odbcDataReader = command.ExecuteReader(); while (odbcDataReader.Read()) { CheckDiv.InnerHtml += "Result: " + odbcDataReader.GetString(6) + "<br/>"; } 

I think you need to set up a working ODBC connection before trying to use the code to just make sure that the driver can be located normally or at least look in the list of available ODBC drivers.

+3
source

I assume that you can connect directly to the registry, but you can also call odbcconf.exe , which is the standard Windows utility. Here is the MSDN link

Back when I learned how to manage ODBC connections, I added the following:

 odbcconf.exe /a {CONFIGSYSDSN "SQL Server" "DSN=?|Description=?|SERVER=?(local)|Trusted_Connection=no|Database=?"} 

Of course you would replace ? their own parameters.

+2
source

Source: https://habr.com/ru/post/1333169/


All Articles