简体   繁体   English

使用 Oracle.ManagedDataAccess 异常连接到 Oracle

[英]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.我在我的 C# (>.NET 4.0) 项目中使用 Oracle.ManagedDataAccess Nuget Package Version 12.2.1100。 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异常消息:ORA-12154:TNS:无法解析指定的连接标识符异常源:Oracle Data Provider for .NET, Managed Driver

Now I thought the ManagedDataAcess contained everything I needed.现在我认为 ManagedDataAccess 包含了我需要的一切。 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注意:我的 Web.config 中没有<oracle.manageddataaccess.client>标签

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.我已经确认 TNS_ADMIN 变量是在控制面板中设置的,但这似乎不起作用。

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.然后我将 tnsnames.ora 文件添加到 bin 文件夹中,我已经让它工作了,但这不是一个长期的解决方案。

Your program does not find the tnsnames.ora (resp. sqlnet.ora ) file.您的程序未找到tnsnames.ora (resp. sqlnet.ora ) 文件。 There are several possibilities to specify the location.有几种可能来指定位置。

  • Define it in .NET config file ( web.config , machine.config , application.config )在 .NET 配置文件( web.configmachine.configapplication.config )中定义它

  • Set environment variable TNS_ADMIN设置环境变量TNS_ADMIN

  • Copy tnsnames.ora , sqlnet.ora files to directory where your application .exe is located.tnsnames.orasqlnet.ora文件复制到您的应用程序 .exe 所在的目录。

Example for .NET config file: .NET 配置文件示例:

<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.注意,不像其他司机/提供商ODP.NET管理的驱动程序TNS_ADMIN从注册表设置。

You probably don't have TNS configured, which is why that form of connection string isn't working.您可能没有配置 TNS,这就是该连接字符串形式不起作用的原因。 You don't need TNS configured if you use a different form of connection string, ex:如果您使用不同形式的连接字符串,则不需要配置 TNS,例如:

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.用您的值替换所有My*占位符。

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).当 Oracle Data Provider for .NET 安装在 Web 服务器上并根据您在安装过程中选择的内容时,它会将条目写入 machine.config 文件(位于 C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\Config \\ 取决于框架版本)。 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)似乎不同版本的 ODP.NET 安装程序对 machine.config 做不同的事情(我们的一些服务器没有在 machine.config 中指定版本号,而其他服务器则为 oracle 管理的客户端指定了特定的版本号)

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.这些条目包含一个特定的版本号,指的是 ODP.NET oracle 客户端版本。 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我们正在使用与 4.121.2.0 不匹配的托管客户端库(版本 12.2.1100 nuget 包)的 4.122.1.0 版构建我们的应用程序

We changed the above entries by removing the oracle managed client library version from this tag:我们通过从此标记中删除 oracle 管理的客户端库版本来更改上述条目:

<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.您还可以从 machine.config 中删除所有这些条目(如果它们存在)并根据您的配置在应用程序 web.config 中指定它们。

A simple way in my case.就我而言,这是一个简单的方法。
Set ORACLE_HOME environment variable in Program.cs(Entrypoint class)在 Program.cs(入口点类)中设置 ORACLE_HOME 环境变量
without any changes or settings in app.config在 app.config 中没有任何更改或设置

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM