Analisando tnsnames.ora no Visual C # 2008
-
12-09-2019 - |
Pergunta
Como eu analisar tnsnames.ora arquivo usando o Visual C # (Studio 2008 Edição Visual Express) para obter os tnsnames? Por exemplo, meu arquivo tnsnames.ora contém
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)
)
)
Como posso analisar este arquivo para obter o TNSNAMES (ie, ORCL, BILL etc). Perdoe-me se esta pergunta soa óbvio demais, estou aprendendo e tentando minha mão em C #
Solução
Em primeiro lugar, você precisará a sintaxe regras para esse arquivo .
Há provavelmente um hack para isso, mas eu pessoalmente ir com um processador completo, como ANTLR combinado com gramática (a lista completa das gramáticas ANTLR pode ser encontrada aqui ).
Outras dicas
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;
}
juntamente com aqueles fornecidos pelo Sathya, criar um método:
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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow