打开Oracle OleDb连接成功,而托管驱动程序连接失败

时间:2016-04-13 17:09:12

标签: c# .net oracle oracle-manageddataaccess

从C#我使用OleDb连接到Oracle,执行以下操作:

String connectionString = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=x;Password=y;Data Source=z";

var connection = new System.Data.OleDbConnection(connectionString);
connection.Open();

工作正常。

在同一过程中,我尝试使用托管驱动程序进行连接,执行以下操作:

String connectionString = "USER ID=x;PASSWORD=y;DATA SOURCE=z";

var connection = new Oracle.ManagedDataAccess.Client.OracleConnection(connectionString);
connection.Open();

失败并出现以下错误:

Oracle.ManagedDataAccess.Client.OracleException (0x80004005): ORA-12154: TNS:could not resolve the connect identifier specified ---> OracleInternal.Network.NetworkException (0x00002F7A): ORA-12154: TNS:could not resolve the connect identifier specified
   at OracleInternal.Network.AddressResolution..ctor(String TNSAlias, String instanceName)
   at OracleInternal.Network.OracleCommunication.DoConnect(String tnsDescriptor)
   at OracleInternal.ServiceObjects.OracleConnectionImpl.Connect(ConnectionString cs, Boolean bOpenEndUserSession, String instanceName)

Server stack trace: 
   at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)
   at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)
   at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword)
   at Oracle.ManagedDataAccess.Client.OracleConnection.Open()

过去,当tnsnames存在问题时,我收到了类似的错误,但我从未遇到过托管驱动程序没有OleDb工作的情况。

我已经仔细检查过,两个连接字符串中的用户,密码和数据源是相同的。

2 个答案:

答案 0 :(得分:0)

听起来你的TNSNames没有正确设置。您也可以使用这样的连接字符串来使用tnsnames

static string constr = @"Data Source=(DESCRIPTION=
         (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=yourhostname )(PORT=1521)))
         (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));
        User Id=system ;Password=yourpasswrd"; 

this is error ORA-12154: TNS:could not resolve the connect identifier specified?

答案 1 :(得分:0)

ODP.NET托管驱动程序使用不同的方式查找tnsnames.ora,resp。比{OraOLEDB} sqlnet.ora。{/ p>

例如,OraOLEDB读取注册表中的TNS_ADMIN值,而ODP.NET托管驱动程序则不会。有关详细信息,请参阅Determining locatation of relevant tnsnames.ora file

一种解决方案是使用文件夹名称设置环境变量TNS_ADMIN,其中tnsnames.orasqlnet.ora文件位于其中。据我所知,环境变量优先于(几乎)ODP.NET托管驱动程序和OraOLEDB中的所有其他设置。