title: ORA-12154 error trying to connect using ODP .NET
body: <p>ORA-12154 error trying to connect using ODP .NET </p>
  
  <p><strong>UPDATE: Wernfried's answer fixed it for me.</strong></p>
  
  <blockquote>
    <p>Create an environment variable
    TNS_ADMIN=D:\oracle\product\12.1.0\dbhome_1\network\admin</p>
    
    <p>Sqlplus reads TNS_ADMIN from Registry but ODP.NET Managed Driver does
    not read the registry. See also:
    <a href="https://stackoverflow.com/questions/33783902/odbcconnection-returning-chinese-characters-as/33790600#33790600">OdbcConnection returning Chinese Characters as &quot;?&quot;</a></p>
  </blockquote>
  
  <p>You can check that the environment variable is set by:</p>
  
  <pre><code>string tns_admin = Environment.GetEnvironmentVariable("TNS_ADMIN")
  </code></pre>
  
  <p>I didn't quite follow how the link he suggested was relevant.</p>
  
  <p><strong>Original question:</strong></p>
  
  <p>ORA-12154 error trying to connect using ODP .NET </p>
  
  <p>The code:</p>
  
  <pre><code>  OracleConnection oracleConnection = new OracleConnection();
    string connectionString = "User Id=redacted;Password=redacted;Data Source=db6";
    oracleConnection.ConnectionString = connectionString;
    oracleConnection.Open();
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&gt; Oracle.ManagedDataAccess.Client.OracleException: ORA-12154: TNS:could not resolve the connect identifier specified ---&gt; 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 
  </code></pre>
  
  <p>My tnsnames.ora:</p>
  
  <pre><code># 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)
      )
    )
  </code></pre>
  
  <p>Connecting with sql-plus:</p>
  
  <pre><code>C:\Users\Derek.Morin\Documents\Visual Studio 2010\Projects\ScriptCode\Oracle&gt;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
  </code></pre>