4

How I parse tnsnames.ora file using Visual C# (Visual Studio 2008 Express edition) to get the tnsnames ? For instance, my tnsnames.ora file contains

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = shaman)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )
BILL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.10.58)(PORT = 1522))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

How can I parse this file to get the TNSNAMES (ie, ORCL, BILL etc). Forgive me if this question sounds too obvious, I'm learning & trying my hand in C#

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134

4 Answers4

4

First of all, you will need the syntax rules for this file.

There is probably a hack for this, but I would personally go with a full parser, like ANTLR combined with the proper grammar (a complete list of ANTLR grammars can be found here).

Mac
  • 8,191
  • 4
  • 40
  • 51
1
public List<string> ReadTextFile(string FP)
{

    string inputString;
    List<string> List = new List<string>();

    try
    {
        StreamReader streamReader = File.OpenText(FP.Trim()); // FP is the filepath of TNS file

        inputString = streamReader.ReadToEnd();
        string[] temp = inputString.Split(new string[] {Environment.NewLine},StringSplitOptions.None);

        for (int i = 0; i < temp.Length ;i++ )
        {
            if (temp[i].Trim(' ', '(').Contains("DESCRIPTION"))
            {                   
                string DS = temp[i-1].Trim('=', ' ');
                List.Add(DS);
            }             

        }
        streamReader.Close();
    }
    catch (Exception EX)
    {
    }


    return List;

}
animuson
  • 53,861
  • 28
  • 137
  • 147
  • 1
    OP mentioned he is fairly new to C#. Maybe adding some explanation and rationale could be helpful to avoid copy/pasting code that isn't fully understood. – shenles Nov 05 '12 at 17:28
0

together with those provided by Sathya, create a method:

StringBuilder strTns = new StringBuilder ();

foreach ( var fileLine in System.IO.File.ReadAllLines(fiTNS.FullName ) )
{
    if ( (fileLine.Length > 0 
           && fileLine.Trim().Substring(0,1) != "#" 
          )
          && ( fileLine.Length > 0 
                && fileLine.Trim ().Substring (0,1) != "/" 
              )
        )

        {
           strTns.AppendFormat("{0}{1}", fileLine, Environment.NewLine);
        }
}

//TNSNamesValues = System.IO.File.ReadAllText (fiTNS.FullName).ToString ().Replace ("\n", "" ).Replace ("\r", "");
String[] splitSeparator = LoadTNSNames (OracleHomeRegistryKey).ToArray ();
String[] TNS = strTns.ToString().Split (splitSeparator, StringSplitOptions.None);
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
0

This code works for me, perhaps we can refactor this to work with multiple entries or create reusable function.

    //Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=49161))(CONNECT_DATA=(SERVICE_NAME=xe)));User Id=myuser;Password=mypassword;
    var stringParts = _connectionString.Split('(');

    //HOST=localhost)
    var hostPair = stringParts.First(s => s.Contains("HOST")).Split("=");
    var host = hostPair[1].Substring(0, hostPair[1].IndexOf(")"));

    //PORT=49161)
    var portPair = stringParts.First(s => s.Contains("PORT")).Split("=");
    var port = portPair[1].Substring(0, portPair[1].IndexOf(")"));

    //SERVICE_NAME=xe)
    var serviceNamePair = stringParts.First(s => s.Contains("SERVICE_NAME")).Split("=");
    var serviceName = serviceNamePair[1].Substring(0, serviceNamePair[1].IndexOf(")"));
Dharman
  • 30,962
  • 25
  • 85
  • 135
rdagumampan
  • 459
  • 4
  • 16