specify TNSNAMES for one program

Monday I wrote on tnsping.exe inconsistencies. Actually there is one good thing in having Oracle Client on Windows looking in the current directory first : you can set one tnsnames for a specific shortcut ! It is quite a viable alternative to .bat files with set TNS_ADMIN=path.

Demo :

First I create a small EXE in C#


using System;
using System.Threading;
using Oracle.DataAccess.Client;

class HelloWorld
  static void Main() {
    OracleConnection connection=
      new OracleConnection("Data Source=DB01; User Id=scott; password=tiger");
    try {
      Console.WriteLine("Msg: " + (new OracleCommand(
        "select * from global_name",connection)).ExecuteScalar());
    } catch(Exception e) {
      Console.WriteLine("Exception Occured :{0}",e.Message);
    } finally {


C:\Windows\Microsoft.NET\Framework\v4.0.*\csc.exe /R:C:\oracle\product\11.2.0\client_1\odp.net\bin\4\Oracle.DataAccess.dll HelloWorld.cs

Create a specific tnsnames and sqlnet






C:\TEMP> HelloWorld

To create a desktop icon with the correct WorkingDirectory with powershell

PS> $ws =  New-Object -comObject WScript.Shell
PS> $desktop = [Environment]::GetFolderPath("Desktop")
PS> $s = $ws.createshortcut($desktop+"\HelloWorld.lnk")
PS> $s.TargetPath = "C:\TEMP\HelloWorld.exe"
PS> $s.WorkingDirectory = "C:\TEMP"
PS> $s.Save()

Author: Laurent Schneider

Oracle Certified Master

1 thought on “specify TNSNAMES for one program”

  1. Hi,

    As while creating OracleConnection using DataAccess provider; it search tnsnames.ora file starting from .net configuration to exe path and then go to admin folder of local drive specified in TNS_ADMIN evnironment variable.

    When I create oracleConnection it goes to tnsnames.ora sepecified in TNS_ADMIN by default; if nothing found in the search order.

    What I am looking at; when the default path does not have connection entry in the tnsnames.ora then how can we define the new tnsnames.ora file path for oracle connection.

    I tried setting the different path if any exception occurrs with default tnsnames in TNS_ADMIN; but it does’t take; I concluded as; once drivers get loaded in AppDomain and it first tries to create the Oracle Connection form default or any spefied TNS_ADMIN location then it does not research in same order; it directly jump to the loaded TNS_ADMIN path.

    Is there any way to override the search pattern of DataAccess provider to take the tnsnames.ora file path everytime while creating oracle connection.


Leave a Reply