user94614
user94614

Reputation: 521

Connection to Oracle using Oracle.ManagedDataAccess exception

I'm using the Oracle.ManagedDataAccess Nuget Package Version 12.2.1100 in my C# (>.NET 4.0) project. Everything works ok in my localhost but on the dev server I'm hit with this exception:

Exception Message: ORA-12154: TNS:could not resolve the connect identifier specified Exception Source: Oracle Data Provider for .NET, Managed Driver

Now I thought the ManagedDataAcess contained everything I needed. Am I missing something else? Is something else interfering with the package? Do I need to add something else?

Note: there is no <oracle.manageddataaccess.client> tag in my Web.config

Code:

<connectionStrings>
   <add name="XXX" connectionString="Data Source=XXX;User ID=XXX;Password=XXX" />
</connectionStrings>

EDIT:

I've confirmed that the TNS_ADMIN variable is set within Control Panel but that didn't seem to do the trick.

I then added the tnsnames.ora file to the bin folder and I've got it working but it isn't a long term solution.

Upvotes: 4

Views: 18482

Answers (4)

Kim Ki Won
Kim Ki Won

Reputation: 1855

A simple way in my case.
Set ORACLE_HOME environment variable in Program.cs(Entrypoint class)
without any changes or settings in app.config

[STAThread]
static int Main()
{
    var oracleHome = GetOracleHome(); // Find registry...
    Environment.SetEnvironmentVariable("ORACLE_HOME", oracleHome);

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    ...

Upvotes: 0

Barry C
Barry C

Reputation: 21

We were seeing similar issues in one of our environments and the following solved our problem;

When Oracle Data Provider for .NET is installed on a web server and depending on what you select during install, it writes entries into the machine.config file (located at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\ depending on the framework version). It seems different versions of the ODP.NET installer do different things to the machine.config (some of our servers have no version number specified in the machine.config and others have a specific version number specified for the oracle managed client)

Depending on the version installed, it adds a couple of lines like this:

...

<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />

...

<oracle.manageddataaccess.client>
  <version number="4.121.2.0">
    <settings>
      <setting name="TNS_ADMIN" value="C:\oracle\client\product\12.1.0\client_1\network\admin" />
    </settings>
  </version>
</oracle.manageddataaccess.client>

...

The entries contain a specific version number referring to the ODP.NET oracle client version. We are building our applications with version 4.122.1.0 of the managed client library (Version 12.2.1100 nuget package) which doesn't match 4.121.2.0

We changed the above entries by removing the oracle managed client library version from this tag:

<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess" />

And specifying a '*' for the version number for this tag:

<oracle.manageddataaccess.client>
    <version number="*">
      <settings>
        <setting name="TNS_ADMIN" value="C:\oracle\client\product\12.1.0\client_1\network\admin" />
      </settings>
    </version>
  </oracle.manageddataaccess.client>

If you need specific version numbers specified then ensure your code is compliled with the same version numbers.

You can also remove all these entries from machine.config if they are there and specify them in the applications web.config depending on your configuration.

Upvotes: 2

mason
mason

Reputation: 32728

You probably don't have TNS configured, which is why that form of connection string isn't working. You don't need TNS configured if you use a different form of connection string, ex:

Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=MyIpOrServerName)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=MySID)));User Id=MyUsername;Password=MyPassword;

Replace all the My* placeholders with your values.

Upvotes: 7

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59642

Your program does not find the tnsnames.ora (resp. sqlnet.ora) file. There are several possibilities to specify the location.

  • Define it in .NET config file (web.config, machine.config, application.config)

  • Set environment variable TNS_ADMIN

  • Copy tnsnames.ora, sqlnet.ora files to directory where your application .exe is located.

Example for .NET config file:

<oracle.manageddataaccess.client>
  <version number="4.122.*">
     <settings>
          <setting name="TNS_ADMIN" value="C:\oracle\network\admin"/>
     </settings>
  </version>
</oracle.manageddataaccess.client>

Note, unlike other drivers/providers the ODP.NET Managed driver does not read the TNS_ADMIN setting from Registry.

Upvotes: 7

Related Questions