5

I have a program in C# that use ODP.NET dlls:

oci.dll, ociw32.dll, Oracle.DataAccess.dll,
orannzsbb11.dll, oraocci11.dll, oraociicus11.dll,
OraOps11w.dll. 

I've got 2 computers. First with whole ODAC package installed, and second without that package. But I have all required dlls in my exe directory, so ODAC is not a problem I think.

The difference between these computers is the path to the TNSNAMES file.

First: C:\app\OraHome_1\Network\admin\
Second: C:\Oracle\product\11.2.0\client_1\network\admin

On the first computer, the program works fine. But on the second one with the same connection string, I get the error:

cannot open connection (ORA-12154)

Using SQL Plus I can connect on both computers. How can I show my program the proper path to the tnsnames.ora file?

2
  • It is where the client was installed.
    – SQLMason
    May 16, 2012 at 12:29
  • 1
    so, everywhere i want to use my program, client has to be installed in the same place? (has the same path) ?
    – Marshall
    May 16, 2012 at 12:32

3 Answers 3

13

You can set the TNS_ADMIN environment variable programmatically. See this page for a step by step. That is if you wanted to change to a specific TNS_NAMES.ORA file. The Oracle Client must still be installed on the client machine.

From ConnectionStrings - without using TNS:

Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;


EDIT: Added 3rd option

Please see this question which could aid you in finding the current location of the client's TNS_NAMES.ORA file - which you could open and modify if you wish (add your own connection if it doesn't exist)

9
  • I too got the idea from @Marshall to not use TNS and connect by IP.
    – SQLMason
    May 16, 2012 at 12:38
  • Thanks, but I need TNSNAMES file - I have many.. many database and this is very dynamic project. People adding and removing databases from there. But anyway: I need tnsname file, I have many computers with (perheps) diffrent path to oracle clients and without ODP.NET installed, and I have program in C#, which use these dlls: dlls: oci.dll, ociw32.dll, Oracle.DataAccess.dll, orannzsbb11.dll, oraocci11.dll, oraociicus11.dll, OraOps11w.dll. So the solution with TNS_ADMIN variable will be ok, right?
    – Marshall
    May 16, 2012 at 12:49
  • If you are installing your own TNS_NAMES.ORA file (sending it along with the install) you could do that. However, you may screw up other people's entries - if they have their own custom entries. The safest way would be connect by IP if possible, the environment variable is an option which specifically answers your question.
    – SQLMason
    May 16, 2012 at 12:53
  • actually, it worked. With 'full' connection string (without tnsnames) it worked... but, as I mentioned, I would like to have thsnames file. For egzample: When I want to test my application with new database. Until now I just apply tnsnames.ora file.. and now I will have to rebuild application (after add another connection string).
    – Marshall
    May 16, 2012 at 13:23
  • @Marshall Please see my edit which would allow you to add your entry to an existing TNS_NAMES.ORA file - no matter where the client has the Oracle Home. You could use the #if (DEBUG == true) compiler directive to change it.
    – SQLMason
    May 16, 2012 at 13:35
3

You don't need to care about the path of your TNSNames file: it'll be automatically discovered by the library itself... once you have it installed. That's the key point: distributing the dll within your project is not enough. You need to install ODP.Net on the machine that need to use it: actually the installation simply create a few registry entry, and one of them point to the right oracle dir (in which the library can find out the tnsnames when needed).

Morover, as someone pointed out, you don't need a tnsnams file at all. You could write everything needed inside the connection string. Here's one I use in my environment:

Data Source= (DESCRIPTION =
      (ENABLE = BROKEN)
      (ADDRESS_LIST =
      (LOAD_BALANCE = ON)
      (FAILOVER = ON)
      (ADDRESS = (PROTOCOL = TCP)(Host =por10srv-a)(Port = 1521))
      )
      (CONNECT_DATA =
      (SERVICE_NAME = por10.gruppo.autostrade.it)
      (FAILOVER_MODE =
      (TYPE = SELECT)
      (METHOD = BASIC)
      (RETRIES = 10)
      (DELAY = 3)
      )
      )
      );User ID=npa_collaudo;Password=npa_collaudo;
3
  • Well.. I think install ODP.NET is not necessary. I was.. I'm trying to avoid installing it. But now.. i'm not sure
    – Marshall
    May 16, 2012 at 12:38
  • Look at my comment to Dan Andrews's answer.
    – Marshall
    May 16, 2012 at 12:51
  • You always need TNSNAMES.ORA if you want to rely on workstations' configuratons... My experience shows that the best way to do so is to loop through Environment.Path entries and append "\..\Network\ADMIN\TNSNAMES.ORA" to find out the right one used by the InstantClient. This seems to be the most valuable way to find out we've been using yet.
    – MensSana
    Jan 15, 2014 at 14:35
2

You don't need to install ODP.NET (or for that matter the Oracle Client) as you seem to have the required DLLs for a local distributable inline oracle client. In your case it's possible to have the TNSNAMES.ORA file located in the same folder as your executable and your specialised "inline oracle client" will pick it up from there. Otherwise the oracle client local to your application will try to pick it up from any client installed on the machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.