开发者

Change/Create ODBC connection from code

I'm working against a Informix DB from C# ASP .Net 4.0 via an ODBC connection. This database does change frequently so I read out what DBs that are installed from the sysmaster table.

Depending on choice my user makes I need to setup a ODBC connection to the choosen DB or change the current connection to change from the systmaster DB to the one choosen.

Anyone got any idea how to do this? I suspect it must be possible to setup a temporary ODBC connection. Also in Visual Studio under the properties for my ODBC connection I do have a connection string looking like this:

Dsn=Informix;uid=xxxxx;database=sysmaster;host=10.10.10.10;srvr开发者_StackOverflow=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 have looked around for a library to connect directly without the ODBC to informix but with now success.

Thanks, Stefan


I have worked out a working solution that is quite nice. I did not realise .NET had full support to do this from code behind without actually modifying the 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 guess you have to setup a working ODBC connection one before trying the code behind to just be sure the Driver can be located ok or at least look in the list of available ODBC drivers.


I suppose you could mess with the registry directly, but you could also call odbcconf.exe, which is a standard Windows utility. Here's an MSDN link

Back when I was looking into how to manipulate ODBC connections, I did something like the following to add a connection:

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

Of course, you'd replace the ? with your own parameters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜