oracle ASP.NET : TNS issue
Question
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))
)
)
Solution
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;
OTHER TIPS
Try using TNSPING to diagnose your issue. It's a utility found in your oracle home/bin directory.
What is the EZConnect adapter? Maybe TNSPing and your code are using different drivers?
Try using the IP address of your database in the connection string in your code. This might show you what's causing the problem.
When I try to use myComputer.myDomain as Data Source in my connection string it gives the error code
ORA-12504 : TNS : the listener processus didn't obtain SERVICE_NAME in CONNECT_DATA
.
Idk what EZConnect is :-)
the command tnsping localhost give this :
C:\app\Administrator\product\11.2.0\dbhome_1\network\admin\sqlnet.ora
Adaptateur TNSNAMES utilisé pour la résolution de l'alias
Tentative de contact de (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myComputer.myDomain)(PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = ORACLEDB)))
OK (20 msec)
Maybe it could help : when I try to connect my DB using SQL Plus, it works fine ...
Thanks a lot for your help.