Question

Comment je parse tnsnames.ora fichier à l'aide pour obtenir les tnsnames Visual C # (Visual Studio 2008 Express Edition)? Par exemple, mon fichier tnsnames.ora contient

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)
    )
  )

Comment puis-je analyser ce fichier pour obtenir le TNSNAMES (c.-à-ORCL, BILL, etc.). Pardonnez-moi si cette question semble trop évident, j'apprends et essayer ma main en C #

Était-ce utile?

La solution

Tout d'abord, vous aurez besoin la syntaxe règles de ce fichier .

Il y a probablement un hack pour cela, mais je voudrais aller personnellement avec un analyseur complet, comme ANTLR combiné avec la grammaire (la liste complète des grammaires ANTLR Vous trouverez ici ).

Autres conseils

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;

}

en même temps que ceux fournis par Sathya, créer une méthode:

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top