【问题标题】:ORA-12154 error trying to connect using ODP .NET尝试使用 ODP .NET 连接时出现 ORA-12154 错误
【发布时间】:2016-03-16 08:52:49
【问题描述】:

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

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

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

Sqlplus 从注册表读取 TNS_ADMIN 但 ODP.NET 托管驱动程序读取 不读注册表。也可以看看: OdbcConnection returning Chinese Characters as "?"

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

string tns_admin = Environment.GetEnvironmentVariable("TNS_ADMIN")

我不太了解他建议的链接的相关性。

原问题:

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

代码:

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

错误:

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 

我的 tnsnames.ora:

# tnsnames.ora Network Configuration File: D:\oracle\product\12.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)
    )
  )

使用 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

【问题讨论】:

  • 您的机器上是否配置了多个 Oracle Home?
  • 据我所知只有一个。除非通过 nuget 安装 odp .net 创建了另一个。
  • 我注意到您通过sqlplus 的连接是使用@localhost/db6 完成的。您能否仅使用@db6sqlplus 连接?您是否也尝试过从命令行运行tnsping db6
  • 创建环境变量TNS_ADMIN=D:\oracle\product\12.1.0\dbhome_1\network\admin。 sqlplus 从注册表读取 TNS_ADMIN 但 ODP.NET 托管驱动程序不读取注册表。另请参阅stackoverflow.com/questions/28280883/…
  • 仅使用 @db6 即可。 tnsping db6 给出 OK(10 毫秒)

标签: oracle odp.net


【解决方案1】:

Wernfried 的评论为我解决了这个问题。由于他没有回来发布它作为答案 - 我将用他的评论结束这个。

创建环境变量

TNS_ADMIN=D:\oracle\product\12.1.0\dbhome_1\network\admin

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

【讨论】:

  • 或者您的 TNS_ADMIN 环境变量已到位,但指向错误的位置,就像在我的情况下所做的那样:C:\ORACLE\ORA12C\NETWORK\ADMIN 而不是 C:\oracle\product\12.2。 0\client_1\Network\Admin
【解决方案2】:

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

【讨论】:

  • 在现代版本的 ODP.NET 中,我们开始依赖配置文件条目,所以发生在你身上的是 devenv.exe.config 或 machine.config 中有一个 TNS_ADMIN 条目。
  • 这又发生了,这次是 machine.config 文件中的错误设置。更新了它(需要在管理模式下打开编辑器)并解决了问题!谢谢克里斯蒂安!
【解决方案3】:

我赞同 Jeff 的观察。这几天我一直在尝试解决这个问题,这就是关键。我搜索了相关服务器上所有现存的 tnsnames.ora 文件,重命名了所有不在 %ORACLE_HOME/network/admin 中的文件,一切都刚刚开始工作!

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

【讨论】:

  • 在现代版本的 ODP.NET 中,我们开始依赖配置文件条目,因此您可能遇到的情况是 devenv.exe.config 或 machine.config 中有 TNS_ADMIN 条目。跨度>
猜你喜欢
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多