2014-03-12 67 views
2

我有一個.csv文件看起來像這樣:刪除行開始在CSV文件在SSIS Controlflow腳本任務

#Example Company       
#(999) 999-9999      
#http://yourwebsite.com        
#Report Date Range: Dec 26, 2013 - Dec 26, 2013      
#Exported: Dec 26, 2013        
#Twitter : Profile Summary        
#Screen Name,Name,Description,Location,Followers,Following,Listed 

SctaSa,statisticalgraph,statistical Screen- The official account for your 
organization,Saudi Arabia,6775,8,75 

所以,我需要從.csv文件採取具體的數據具有可讀性到SSIS轉型,從"Screen Name"柱和remove the garbage data與#做起,要像她那樣

Screen Name,Name,Description,Location,Followers,Following,Listed,Exported,Report Date Range 
SctaSa,statisticalgraph,statistical Screen- The official account for your organization,Saudi Arabia,6775,8,75,26-Dec-13,26-Dec-13 

我試圖用這個C#腳本,但它並沒有穿文件(我不是在C#中的專家,所以我不不知道是什麼問題是)我試圖用下面的腳本刪除任何以# but the file dose not transfare to the out put path開頭的行;你能給我任何建議嗎?!

#region Namespaces 
using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Runtime; 
using System.Windows.Forms; 
using System.IO; 
using System.Collections.Generic; 
#endregion 

namespace ST_a7b941606e0b40aa920bfe13fc81dc81 
{ 
    /// <summary> 
    /// ScriptMain is the entry point class of the script. Do not change the name, attributes, 
    /// or parent of this class. 
    /// </summary> 
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute] 
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      var lines = new List<string>(); 
      string line; 
      using (var file = new System.IO.StreamReader("D:\\try.csv")) 
      { 
       while ((line = file.ReadLine()) != null) 
       { 
        if (line.Length != 0) 
        { 
         if (!line.StartsWith("#") ) 
         { 
          lines.Add(line); 
         } 
        } 
       } 
      } 
      File.WriteAllLines("D:\\SCTA_ETL\\try.csv", lines); 
     } 

     /// <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() 
     { 
      // TODO: Add your code here 

      Dts.TaskResult = (int)ScriptResults.Success; 
     } 

     #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 

    } 
} 
+1

請,不包括有關在問題的標題使用,除非它不會沒有很有意義語言信息。標籤用於此目的。 –

回答

1

你可能想改變在中間你的邏輯:

var lines = new List<string>(); 
string outputPath = // your output path here 
using (var file = new System.IO.StreamReader("c:\\mycsv.csv")) 
{ 
    string line; 
    while ((line = file.ReadLine()) != null) 
    { 
    if (!line.StartsWith("#")) 
    { 
     lines.Add(line); 
    } 
    } 
} 
File.WriteAllLines(outputPath, lines); 

您已經移除了「#」裏面的任何地方都行。

相反,只添加不以「#」開頭的行。

此外,請務必在完成後關閉並處理您的StreamReader,或者將整個事件放入using部分。

+0

拜託,你能寫信給我整個劇本直接執行嗎? :) – user3143565

+0

我添加了完整的腳本 –

+0

我用你的腳本,但它不會傳輸文件輸出路徑, – user3143565

3

另一種方式:

File.WriteAllLines(outputPath, File.ReadAllLines("c:\\mycsv.csv").Where(x => !x.StartsWith("#")).ToArray()); 
+0

請你能寫信給我直接執行它的整個腳本嗎? :) – user3143565

+0

我的代碼替換base.PreExecute()後的所有腳本; –

+0

您可以將代碼封裝到try/catch塊來處理異常。 –