2017-03-12 73 views
0

我正在使用SSIS腳本組件驗證CSV文件內容。具有信息的CSV文件標頭的內容是:FName,LName,電子郵件,地址,城市,州,ZipCode。我希望正則表達式只驗證電子郵件列。有沒有辦法?嘗試在SSIS中使用C#與腳本驗證電子郵件,但希望僅驗證電子郵件列

[Code] 
    public static bool IsValid(string EmailAddress) 
    { 
     string strPattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9] 
     {1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; 

     bool bFlag = false; 
     System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(strPattern); 
     System.Text.RegularExpressions.Match match = regex.Match(EmailAddress); 
     if (match.Success) bFlag = true; 

     return bFlag; 
    } 
    static void TestSeparatingValidTextData() 
    { 
     try 
     { 
      //bool isFirst = true; 
      //using (StreamReader sr = new StreamReader(filePath)) 
      //{ 
      // while (sr.Peek() != -1) 
      // { 
      //  if (isFirst) 
      //  { 
      //   isFirst = false; 
      //   continue; 
      //  } 
      // } 
      //} 
      System.IO.StreamReader objReadFile = new 
      System.IO.StreamReader(@"C:\_BISolutions\ClinicDailyData\Bellevue\ 
      Bellevue_EmailsToValidate\Bellevue_EmailToValidateData.csv"); 
      System.IO.StreamWriter objWriteValidFile = new 
      System.IO.StreamWriter(@"C:\_BISolutions\ 
    ClinicDailyData\Bellevue\Bellevue_ValidEmail\Bellevue_ValidEmailData.csv" 
      , false); 
      System.IO.StreamWriter objWriteInValidFile = new System.IO.StreamWriter(@"C:\_BISolutions\ClinicDailyData\Bellevue\ 
      Bellevue_InValidEmail\Bellevue_InValidEmailData.csv", false); 
      string strLineData = ""; 

      while ((strLineData = objReadFile.ReadLine()) != null) 
      //&& objReadFile.Peek() != -1) 
      {  /*************************/ 
        //This first if statement bypasses the first line. 
        //if (isFirst) 
        //{ 
        // isFirst = false; 
        //   continue; 
        //} 
        /*************************/ 
       if (IsValid(strLineData)) 
       { 
        objWriteValidFile.WriteLine(strLineData); 
       } 
       else 
       { 
        objWriteInValidFile.WriteLine(strLineData); 
       } 
      } 
      objReadFile.Close(); 
      objWriteValidFile.Close(); 
      objWriteInValidFile.Close(); 
     } 
     catch (System.Exception objException) 
     { 
      System.Console.WriteLine(objException.ToString()); 
      throw objException; //Must add this for the Main method to catch 
      this exception. 
     } 

    } 
    /// <summary> 
    /// This method is called when this script task executes in the control 
     flow. 
    /// Before returning from this method, set the value of Dts.TaskResult to 
     indicate success or failure. 
    /// To open Help, press F1. 
    /// </summary> 
    public void Main() 
    { 
     try 
     { 

      TestSeparatingValidTextData(); 
      Dts.TaskResult = (int)ScriptResults.Success; 
     } 
     catch (Exception objException) 
     { 
      string strMessage = @"On Error, please check the file is in 
     C:\_BISolutions\ClinicDailyData\Bellevue\ 
     Folder"; 
      Dts.Events.FireError(0, strMessage, objException.ToString() 
           , string.Empty, 0); 
      Dts.TaskResult = (int)ScriptResults.Failure; 

     } 

    } 

    #region ScriptResults declaration 
    /// <summary> 
    /// This enum provides a convenient shorthand within the scope of this 
     class for setting the 
    /// result of the script. 
    /// 
    /// This code was generated automatically. 
    /// </summary> 
    enum ScriptResults 
    { 
     Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, 
     Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
    }; 
    #endregion 


[/Code] 

我想在csv文件中只有一列有一個名爲email的標題,供RegEx檢查(並非所有列)。任何幫助將不勝感激。

回答

2

您目前正在將您的IsValid函數全部傳遞給您。你需要分割它並傳入你想要的條目。例如,

var firstLine = objReadFile.ReadLine(); // pop off the header line 
objWriteValidFile.WriteLine(firstLine); 
objWriteInValidFile.WriteLine(firstLine); 
while ((strLineData = objReadFile.ReadLine()) != null) 
//&& objReadFile.Peek() != -1) 
{  /*************************/ 
    //This first if statement bypasses the first line. 
    //if (isFirst) 
     //{ 
     // isFirst = false; 
     //   continue; 
     //} 
     /*************************/ 
    var parts = strLineData.Split(','); // assuming that ',' is your delimiter character 
    if (parts.Length >= 2 && IsValid(parts[2])) 
    { 
     // write to valid file 
    } 
    else 
    { 
     // write to invalid file 
... 
+0

嗨丹,是的你是正確的,它的作品真棒!非常感謝!哇,是的,你是正確的目標字符串位置。再一次感謝你! –

+0

@ChrisSingleton你必須[接受答案](http://stackoverflow.com/tour)如果它解決了你的問題 – Yahfoufi

+0

嗨Yahfoufi,你是對的,我確實接受答案。事實上,它太棒了!謝謝你們的幫助!這真的打開了我的眼睛,瞭解如何瞄準專欄! –