oracle ASP.NET : TNS issue

Skilpit picture Skilpit · Jan 4, 2011 · Viewed 11.7k times · Source

I am trying to connect to my DB oracle with ASP.NET/C#.

Here is my code:

OracleConnection connection = new OracleConnection();
connection.ConnectionString = @"Data Source=ORACLEDB;User id=me;Password=xxxx;";
try
{
   connection.Open();
}
catch (Exception ex)
{
    Console.WriteLine("Exception occurs when connecting to DB : " + ex.Message + ex.StackTrace);
}

and here is the error I get:

ORA-12154: TNS : the identifier could not be solved

Here is my TNSNAMES.ora:

ORACLEDB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = myComputer.myDomain)(PORT = 1521))
    (CONNECT_DATA =
      (SERVICE_NAME = ORACLEDB)
    )
  )

I work on a win2k3 server R2.

Oracle is on the same server. It's Oracle 11g.

When I do a tnsping, I get this:

EZCONNECT adapter used to resolved the alias
Tentative de contact de (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST=169.254.216.123)(PORT=1521)))
OK (20 msec)

And here is my listener.ora:

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = CLRExtProc)
      (ORACLE_HOME = C:\app\Administrator\product\11.2.0\dbhome_1)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ONLY:C:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = myComputer.myDomain)(PORT = 1521))
    )
  )

Answer

Skilpit picture Skilpit · Jan 5, 2011

Put the string connection in the web.config:

  <connectionStrings>
    <add name="OracleDatabase"
          connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME= ORACLEDB)));User Id= me;Password= xxx;Persist Security Info=True;"
          providerName="System.Data.OracleClient" />
  </connectionStrings>

And then get the connection string from the web.config:

connection.ConnectionString = ConfigurationManager.ConnectionStrings["OracleDatabase"].ConnectionString;