0

I have tnsnames.ora file which includes 50+ aliases. Added example content below.

Currently I use below code to get aliases from tns;

tnsnamesreadercustom tsc = new tnsnamesreadercustom();
uceTNS.DataSource = tsc.LoadTNSNames(FilePath);

And I parse these tnsnames aliases into combobox successfully like ;

NAME1
NAME2
NAME3

But i couldn't finish the last step which is;

User selects the above aliases for example NAME1 and it should get the details on messagebox like;

HOST:name1.local.com
PORT:1521
SERVICE_NAME:nameone
SID=null

Here is the tnsnames.ora example;

NAME1=
  (DESCRIPTION=
    (ADDRESS=
      (PROTOCOL=TCP)
      (HOST=name1.local.com)
      (PORT=1521)
    )
    (CONNECT_DATA=
      (SERVER=dedicated)
      (SERVICE_NAME=nameone)
    )
  )

NAME2= 
  (DESCRIPTION= 
    (ADDRESS= 
      (PROTOCOL=TCP) 
      (HOST=172.31.60.227) 
      (PORT=1531) 
    ) 
    (CONNECT_DATA= 
      (SERVER=dedicated) 
      (SID=nametwo) 
    ) 
  ) 

NAME3= 
  (DESCRIPTION= 
    (ADDRESS= 
      (PROTOCOL=TCP) 
      (HOST=172.31.70.174) 
      (PORT=1521) 
    ) 
    (CONNECT_DATA= 
      (SERVER=dedicated) 
      (SERVICE_NAME=namethree) 
    ) 
  )

Here is the tnsnamesreadercustom;

   class tnsnamesreadercustom
    {
        public List<string> LoadTNSNames(string strTNSNAMESORAFilePath)
        {
            List<string> DBNamesCollection = new List<string>();
            string RegExPattern = @"[\n][\s]*[^\(][a-zA-Z0-9_.]+[\s]*=[\s]*\(";

            if (!strTNSNAMESORAFilePath.Equals(""))
            {
                //check out that file does physically exists
                System.IO.FileInfo fiTNS = new System.IO.FileInfo(strTNSNAMESORAFilePath);
                if (fiTNS.Exists)
                {
                    if (fiTNS.Length > 0)
                    {
                        //read tnsnames.ora file
                        int iCount;
                        for (iCount = 0; iCount < Regex.Matches(System.IO.File.ReadAllText(fiTNS.FullName), RegExPattern).Count; iCount++)
                        {
                            DBNamesCollection.Add(Regex.Matches(System.IO.File.ReadAllText(fiTNS.FullName), RegExPattern)[iCount].Value.Trim().Substring(0, Regex.Matches(System.IO.File.ReadAllText(fiTNS.FullName), RegExPattern)[iCount].Value.Replace("=", " ").Trim().IndexOf(" ")).Replace(" ", "").Replace("=", "").Replace(Environment.NewLine, ""));
                        }
                    }
                }
            }
            return DBNamesCollection;
        }
    }
john true
  • 263
  • 1
  • 5
  • 26
  • 1
    What is `tnsnamesreadercustom`? Might help if you posted some code. – John Wu Nov 16 '18 at 17:28
  • @ well, it just regexp the aliases, i just shared – john true Nov 16 '18 at 17:35
  • Have you tried [this solution](https://stackoverflow.com/questions/1243022/parsing-tnsnames-ora-in-visual-c-sharp-2008)? – Dean Nov 16 '18 at 17:38
  • @Dean They are parsing aliases not parameters. – john true Nov 16 '18 at 17:40
  • I also found this which might help: https://www.codeproject.com/Tips/224853/Enumerate-entries-in-TNSNames-ora-using-Oracle-pro. – Polyfun Nov 16 '18 at 17:41
  • @Polyfun Thank you but i am not using Oracle.DataAccess.Client. I tried this about 2 days ago. but no luck. – john true Nov 16 '18 at 17:43
  • [This project](https://github.com/espenrl/TnsNames) looks useful as well, and is available as a [NuGet Package](https://www.nuget.org/packages/erl.Oracle.TnsNames/). I'm not sure I understand your point about aliases versus parameters. The linked-to ANTLR grammar looks like it for sure supports parameters. – Dean Nov 16 '18 at 17:50
  • this looks like a TNSNAMES.ora file. Have you looked at this? https://stackoverflow.com/questions/3549863/ – Hanjun Chen Nov 16 '18 at 17:53
  • Side note unrelated to question: Why do you repeatedly read the file (_strTNSNAMESORAFilePath_) again, again, and again (_System.IO.File.ReadAllText_)? Why did you not read it just once into a string variable and then use the string variable? And why do you repeatedly do the same regex pattern match on the same string (file content) again and again, instead of doing it just once and then using the resulting MatchCollection? Not only is it poor performance-wise (doesn't probably matter), but it also is unnecessary code duplication with code readability going out of the window... –  Nov 16 '18 at 18:49

0 Answers0