如何分析(parse)oralce的服务名文件(100)

青云

Unregistered / Unconfirmed
GUEST, unregistred user!
oracle的服务名文件是一个简单的文本,一般目录是:d:/oracle/product/10.2.0/db_1/NETWORK/ADMIN/tnsnames.ora文件内容是:# tnsnames.ora Network Configuration File: G:/oracle/product/10.2.0/db_1/NETWORK/ADMIN/tnsnames.ora# Generated by Oracle configuration tools.STP= (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = dzcnb)(PORT = 1521)) ) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = dzcdb) ) )HOLLEYWMS = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = dzcnb)(PORT = 1521)) ) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = dzcdb) ) )目的是把 STP 和HOLLEYWMS这两个服务名取出来放到Tcombobox里;找了一段java 代码,有兴趣的可以一起翻译成delphi,Date: 19 Feb 2004 13:16:27 -0800Here is a function to parse the TNSNames.ora file to get a list of oracle databases configured on a machine... It's nothing fancy, but I know that I needed it, so, hopefully it will be helpful to someone else. public static string[] GetDatabases() { string output = ""; string fileLine; Stack parens = new Stack(); // open tnsnames.ora StreamReader sr; try { sr = new StreamReader(@"C:/oracle/ora92/network/ADMIN/TNSNAMES.ORA"); } catch (System.IO.FileNotFoundException ex) { throw ex; } // Read the first line of the file fileLine = sr.ReadLine(); // loop through, reading each line of the file while ( fileLine != null) { // if the first non whitespace character is a #, ignore the line // and go to the next line in the file if (fileLine.Length>0 && fileLine.Trim().Substring(0,1) != "#") { // Read through the input line character by character char lineChar; for (int i = 0; i<fileLine.Length; i++) { lineChar = fileLine; if (lineChar == '(') { // if the char is a ( push it onto the stack parens.Push(lineChar); } else if (lineChar == ')') { // if the char is a ), pop the stack parens.Pop(); } else { // if there is nothing in the stack, add the character to the ouput if (parens.Count==0) { output += lineChar; } } } } // Read the next line of the file fileLine = sr.ReadLine(); } // Close the stream reader sr.Close(); // Split the output string into a string[] string [] split = output.Split('='); // trim each string in the array for (int i=0; i<split.Length; i++) { split = split.Trim(); } Array.Sort(split); return split; }
 
http://bbs.2ccc.com/topic.asp?topicid=323320
 
http://www.itpub.net/viewthread.php?tid=1145748&page=1&extra=#pid13520887
 
呵呵,昨天晚上尝试翻译了一把,搞定了,其实很简单;不用堆栈也可以,就弄个计数更简单;//调用显示效果:procedure TForm17.Button1Click(Sender: TObject);begin memResult.Lines:= ParseTnsnames('G:/oracle/product/10.2.0/db_1/NETWORK/ADMIN/tnsnames.ora');end;//分析函数,输入为 tnsnames.ora的绝对路径名,可以从注册表里获取;function TForm17.ParseTnsnames(sFileName: String): TStrings;var output: string; fileLine: string; iGhCnt:integer;// 刮号数量,(加一, )减一; i, j: integer; sListSrc: TStringList; sListDec:TStringList; iLength: integer; lineChar: Char;begin sListSrc:=TStringList.Create; sListDec:=TStringList.Create; try sListSrc.LoadFromFile(sFileName); except FreeAndNil(sListSrc); result:= sListDec; exit; end; iGhCnt:=0; for I := 0 to sListSrc.Count - 1 do begin fileLine := sListSrc; fileLine := trim(fileLine); iLength := length(fileLine); if (Length(fileLine) = 0) or (fileLine[1] = '#') then Continue; for j := 1 to iLength do begin lineChar := fileLine[j]; if lineChar = '(' then inc(iGhCnt) else if (lineChar = ')') then dec(iGhCnt) else if (iGhCnt = 0) then output := output + lineChar; end; end; output:=StringReplace(output,'=',',',[rfReplaceAll]) ; if output='' then begin FreeAndNil(sListSrc); result:= sListDec; exit; end; FreeAndNil(sListSrc); sListDec.CommaText:=output; result:=sListDec;end;
 
顶部