使用odbc连接到c#中的Oracle数据库

时间:2017-03-24 14:32:07

标签: c# oracle odbc

我试图在c#中编写一个连接到Oracle数据库的工具,我需要使用ODBC。

如果我使用以下代码:

using System.Data.Odbc;
string str1 = "DATA SOURCE=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abcd.efgh.net)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service_name)));USER ID=user_id;PASSWORD=qassword";
OdbcConnection coon = new OdbcConnection();
coon.ConnectionString = str1;
coon.Open();

我收到错误消息:

An unhandled exception of type 'System.Data.Odbc.OdbcException' occurred in System.Data.dll
Additional information: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

即使我将连接字符串更改为:

string str2 = "Driver={Oracle in OraClient12home1};DATA SOURCE=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abcd.efgh.net)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service_name)));USER ID=user_id;PASSWORD=qassword";

我收到了同样的错误消息。 所以我猜问题不是" Diver = {...}"部分?但是我在哪里弄错了数据来源"部分?

谢谢。

2 个答案:

答案 0 :(得分:1)

尝试一下

        using System.Data.Odbc;

        String connectionString = "Dsn=HP5ODBC;uid=system;pwd=manager";          
        OdbcConnection coon = new OdbcConnection();
        coon.ConnectionString = connectionString;
        coon.Open();
        MessageBox.Show(coon.State.ToString());

答案 1 :(得分:0)

尝试

Driver={Oracle in OraClient12home1};DBQ={(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abcd.efgh.net)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service_name)))};UID=user_id;PWD=qassword";

而不是

Driver={Oracle in OraClient12home1};DATA SOURCE=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abcd.efgh.net)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service_name)));USER ID=user_id;PASSWORD=qassword";

请参阅Using the Oracle ODBC Driver: Format of the Connection String和/或Oracle in OraClient11g_home1 connection strings