oracle 尝试使用 ODP .NET 连接时出现 ORA-12154 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34206659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:06:26  来源:igfitidea点击:

ORA-12154 error trying to connect using ODP .NET

oracleodp.net

提问by Derek

ORA-12154 error trying to connect using ODP .NET

尝试使用 ODP .NET 连接时出现 ORA-12154 错误

UPDATE: Wernfried's answer fixed it for me.

更新:Wernfried 的回答为我解决了这个问题。

Create an environment variable TNS_ADMIN=D:\oracle\product\12.1.0\dbhome_1\network\admin

Sqlplus reads TNS_ADMIN from Registry but ODP.NET Managed Driver does not read the registry. See also: OdbcConnection returning Chinese Characters as "?"

创建环境变量 TNS_ADMIN=D:\oracle\product\12.1.0\dbhome_1\network\admin

Sqlplus 从注册表读取 TNS_ADMIN 但 ODP.NET 托管驱动程序不读取注册表。另请参阅: OdbcConnection 将汉字返回为“?”

You can check that the environment variable is set by:

您可以通过以下方式检查环境变量是否设置:

string tns_admin = Environment.GetEnvironmentVariable("TNS_ADMIN")

I didn't quite follow how the link he suggested was relevant.

我不太明白他建议的链接是如何相关的。

Original question:

原问题:

ORA-12154 error trying to connect using ODP .NET

尝试使用 ODP .NET 连接时出现 ORA-12154 错误

The code:

编码:

  OracleConnection oracleConnection = new OracleConnection();
  string connectionString = "User Id=redacted;Password=redacted;Data Source=db6";
  oracleConnection.ConnectionString = connectionString;
  oracleConnection.Open();

The error:

错误:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Oracle.ManagedDataAccess.Client.OracleException: ORA-12154: TNS:could not resolve the connect identifier specified ---> OracleInternal.Network.NetworkException: 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.Network.OracleCommunication.Connect(String 

My tnsnames.ora:

我的 tnsnames.ora:

# tnsnames.ora Network Configuration File: D:\oracle\product.1.0\dbhome_1\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.

DB6 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = redacted.redacted.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = db6)
    )
  )

LISTENER_DB6 =
  (ADDRESS = (PROTOCOL = TCP)(HOST = redacted.redacted.com)(PORT = 1521))


ORACLR_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
    (CONNECT_DATA =
      (SID = CLRExtProc)
      (PRESENTATION = RO)
    )
  )

Connecting with sql-plus:

与 sql-plus 连接:

C:\Users\Derek.Morin\Documents\Visual Studio 2010\Projects\ScriptCode\Oracle>sqlplus redacted/redacted@localhost/db6

SQL*Plus: Release 12.1.0.1.0 Production on Thu Dec 10 09:10:14 2015

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Last Successful login time: Tue Sep 22 2015 09:41:19 -05:00

Connected to:
Oracle Database 12c Release 12.1.0.1.0 - 64bit Production

采纳答案by Derek

Wernfried's comment fixed it for me. Since he didn't come back to post it as an answer - I will close this with his comment.

Wernfried 的评论为我修复了它。由于他没有回来将其作为答案发布 - 我将用他的评论结束此评论。

Create an environment variable

创建环境变量

TNS_ADMIN=D:\oracle\product.1.0\dbhome_1\network\admin

sqlplus reads TNS_ADMIN from Registry but ODP.NET Managed Driver does not read the registry. See also stackoverflow.com/questions/28280883/… – Wernfried Domscheit Dec 10 '15 at 16:24

sqlplus 从注册表读取 TNS_ADMIN 但 ODP.NET 托管驱动程序不读取注册表。另请参阅 stackoverflow.com/questions/28280883/... – Wernfried Domscheit 2015 年 12 月 10 日 16:24

回答by Bruce Morrison

I second Jeff's observation. I've been trying to figure this out for a couple of days, and that was the key. I did a search on all extant tnsnames.orafiles on the server in question, renamed every one that wasn't in %ORACLE_HOME/network/admin, and everything just started to work!

我支持杰夫的观察。几天来我一直试图弄清楚这一点,这就是关键。我tnsnames.ora对相关服务器上的所有现存文件进行了搜索,重命名了所有不在%ORACLE_HOME/network/admin.

I recently uninstalled 32 bit Oracle drivers and re-installed 64 bit drivers... for a short period of time, the only tnsnames.orafile was the sample file (in the sample subdirectory)... perhaps the bad link (which HAD to be in the registry, I've had that TNS_ADMINenvironment variable the whole time) was forged at that point? Not sure, but even though the variable appears to help some people, the water can be very muddy in regards to this issue.

我最近卸载了 32 位 Oracle 驱动程序并重新安装了 64 位驱动程序......在短时间内,唯一的tnsnames.ora文件是示例文件(在示例子目录中)......也许是坏链接(它必须在注册表,我一直都有那个TNS_ADMIN环境变量)是在那个时候伪造的?不确定,但即使该变量似乎对某些人有帮助,但在此问题上的水可能非常混浊。

回答by Jeff

Note: I had this problem in an SSRS project using ODP.Net and in my case the ODP.Net defaulted my TNS to "C:\Program Files (x86)\Oracle Developer Tools for VS2015\network\admin" (in spite of the fact I had a TNS_ADMIN environmental variable that was for something else. I went into that directory and renamed the sample tnsnames.ora and sqlnet.ora files to something different (i.e. junk-tnsnames.ora) it started working. Crazy! Hopefully this helps someone else.

注意:我在使用 ODP.Net 的 SSRS 项目中遇到了这个问题,在我的情况下,ODP.Net 默认我的 TNS 为“C:\Program Files (x86)\Oracle Developer Tools for VS2015\network\admin”(尽管事实上,我有一个 TNS_ADMIN 环境变量用于其他目的。我进入该目录并将示例 tnsnames.ora 和 sqlnet.ora 文件重命名为不同的内容(即垃圾 tnsnames.ora),它开始工作。疯狂!希望这个帮助别人。