Search code examples
c#.netoracle-databaseoracle-manageddataaccess

Opening Oracle OleDb connection succeeds, while managed driver connection fails


From C# I'm connecting to Oracle using OleDb doing the following:

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();

That works fine.

From the same process, I'm trying to connect using the managed driver doing the following:

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

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

That fails with the following error:

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()

I've received errors like this in the past when there was a problem with tnsnames, but I've never had a situation where OleDb works when the managed driver doesn't.

I've double checked and the user, password, and datasource are identical in the two connection strings.


Solution

  • ODP.NET Managed driver uses a different way of looking for tnsnames.ora, resp. sqlnet.ora than OraOLEDB does.

    For example, OraOLEDB reads the Registry for TNS_ADMIN value, whereas ODP.NET Managed driver does not. See Determining locatation of relevant tnsnames.ora file for more details.

    One solution is to set an Environment variable TNS_ADMIN with folder name where tnsnames.ora and sqlnet.ora files are located. As far as I know the Environment variables takes precedence over (almost) all other setting in both, ODP.NET Managed driver and OraOLEDB.