Domanda

Come il parsing tnsnames.ora file utilizzando Visual C # (Visual Studio 2008 Express Edition) per ottenere i tnsnames? Per esempio, il mio file tnsnames.ora contiene

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

Come posso analizzare questo file per ottenere il TNSNAMES (vale a dire, ORCL, BILL, ecc). Perdonatemi se questa domanda sembra troppo ovvio, sto imparando e provando la mia mano in C #

È stato utile?

Soluzione

Prima di tutto, è necessario la sintassi regole per questo file .

Non è probabilmente un hack per questo, ma io personalmente andare con un parser completo, come ANTLR combinato con la grammatica corretta (un elenco completo delle grammatiche ANTLR può essere trovato qui ).

Altri suggerimenti

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;

}

insieme a quelli forniti da Sathya, creare un metodo:

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top