代码之家  ›  专栏  ›  技术社区  ›  Sathyajith Bhat ron tornambe

在Visual C#2008中解析tnsnames.ora

  •  3
  • Sathyajith Bhat ron tornambe  · 技术社区  · 15 年前

    例如,我的tnsnames.ora文件包含

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

    3 回复  |  直到 15 年前
        1
  •  4
  •   Mac    15 年前

    the syntax rules for this file .

    可能有人对此进行了攻击,但我个人会使用完整的解析器,如 ANTLR the proper grammar (ANTLR语法的完整列表) can be found here

        2
  •  1
  •   animuson    13 年前
    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;
    
    }
    
        3
  •  0
  •   Sathyajith Bhat ron tornambe    13 年前

    与Sathya提供的方法一起,创建一种方法:

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