2014-02-26 124 views
0

我已經創建了一個將數據從CSV文件複製到SQL Server表的轉換。由於我的CSV文件是動態的,並且在給定時間可以有任意數量的列,因此我包含一個ScriptComponent任務以保持映射B/W 'FlatFileSource''OLEDBDestination'任務。在SSIS腳本組件任務中讀取CSV文件

我現在能夠複製數據,但列名行也被添加爲我不想要的目標表中的數據行。以下是我在腳本任務中編寫的用於讀取csv並將行添加到輸出緩衝區的代碼。

/* Microsoft SQL Server Integration Services Script Component 
* Write scripts using Microsoft Visual C# 2008. 
* ScriptMain is the entry point class of the script.*/ 

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Pipeline.Wrapper; 
using Microsoft.SqlServer.Dts.Runtime.Wrapper; 
using System.IO; 
using System.Text.RegularExpressions; 

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 
public class ScriptMain : UserComponent 
{ 
    private StreamReader sr = null; 


    public override void PreExecute() 
    { 
     base.PreExecute(); 
     /* 
      Add your code here for preprocessing or remove if not needed 
     */ 

     string sourceFile = Variables.FilePath; 

     if (File.Exists(sourceFile)) 
     { 
      try 
      { 
       sr = new StreamReader(sourceFile); 
      } 

      catch 
      { 

      } 
     } 
    } 

    public override void PostExecute() 
    { 
     base.PostExecute(); 
     /* 
      Add your code here for postprocessing or remove if not needed 
      You can set read/write variables here, for example: 
      Variables.MyIntVar = 100 
     */ 
    } 

    public override void Input0_ProcessInputRow(Input0Buffer Row) 
    { 
     /* 
      Add your code here 
     */ 
    } 

    public override void CreateNewOutputRows() 
    { 
     /* 
      Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer". 
      For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput". 
     */ 

     if (sr != null) 
     { 
      while (!sr.EndOfStream) 
      { 
       string headline = sr.ReadLine(); 

       if (!string.IsNullOrEmpty(headline)) 
       { 

        //get columns value by comma as delimited and Double Quotation as text qualifier 

        //char[] param = {',', @'\'}; 

        string[] columns = headline.Split(','); 


        Output0Buffer.AddRow(); 
        Output0Buffer.Source = Variables.Source.ToString(); 
        Output0Buffer.Column = columns.Length > 0 ? columns[0].ToString() : null; 
        Output0Buffer.Column1 = columns.Length > 0 ? columns[1].ToString() : null; 
        Output0Buffer.Column2 = columns.Length > 0 ? columns[2].ToString() : null; 
        Output0Buffer.Column3 = columns.Length > 0 ? columns[3].ToString() : null; 
        Output0Buffer.Column4 = columns.Length > 0 ? columns[4].ToString() : null; 
        Output0Buffer.Column5 = columns.Length > 0 ? columns[5].ToString() : null; 
        Output0Buffer.Column6 = columns.Length > 0 ? columns[6].ToString() : null; 
        Output0Buffer.Column7 = columns.Length > 0 ? columns[7].ToString() : null; 
        Output0Buffer.Column8 = columns.Length > 0 ? columns[8].ToString() : null; 
        Output0Buffer.Column9 = columns.Length > 0 ? columns[9].ToString() : null; 
        Output0Buffer.Column10 = columns.Length > 0 ? columns[10].ToString() : null; 
        Output0Buffer.Column11 = columns.Length > 0 ? columns[11].ToString() : null; 
        Output0Buffer.Column12 = columns.Length > 0 ? columns[12].ToString() : null; 
        Output0Buffer.Column13 = columns.Length > 0 ? columns[13].ToString() : null; 
        Output0Buffer.Column14 = columns.Length > 0 ? columns[14].ToString() : null; 
        Output0Buffer.Column15 = columns.Length > 0 ? columns[15].ToString() : null; 
        Output0Buffer.Column16 = columns.Length > 0 ? columns[16].ToString() : null; 
        Output0Buffer.Column17 = columns.Length > 0 ? columns[17].ToString() : null; 
        Output0Buffer.Column18 = columns.Length > 0 ? columns[18].ToString() : null; 
        Output0Buffer.Column19 = columns.Length > 0 ? columns[19].ToString() : null; 
        Output0Buffer.Column20 = columns.Length > 0 ? columns[20].ToString() : null; 
        Output0Buffer.Column21 = columns.Length > 0 ? columns[21].ToString() : null; 
        Output0Buffer.Column22 = columns.Length > 0 ? columns[22].ToString() : null; 
        Output0Buffer.Column23 = columns.Length > 0 ? columns[23].ToString() : null; 
        Output0Buffer.Column24 = columns.Length > 0 ? columns[24].ToString() : null; 
        Output0Buffer.Column25 = columns.Length > 0 ? columns[25].ToString() : null; 
        Output0Buffer.Column26 = columns.Length > 0 ? columns[26].ToString() : null; 
        Output0Buffer.Column27 = columns.Length > 0 ? columns[27].ToString() : null; 
        Output0Buffer.Column28 = columns.Length > 0 ? columns[28].ToString() : null; 
        Output0Buffer.Column29 = columns.Length > 0 ? columns[29].ToString() : null; 
        Output0Buffer.Column30 = columns.Length > 0 ? columns[30].ToString() : null; 
        Output0Buffer.Column31 = columns.Length > 0 ? columns[31].ToString() : null; 
        Output0Buffer.Column32 = columns.Length > 0 ? columns[32].ToString() : null; 
        Output0Buffer.Column33 = columns.Length > 0 ? columns[33].ToString() : null; 
        Output0Buffer.Column34 = columns.Length > 0 ? columns[34].ToString() : null; 
        Output0Buffer.Column35 = columns.Length > 0 ? columns[35].ToString() : null; 
        Output0Buffer.Column36 = columns.Length > 0 ? columns[36].ToString() : null; 
        Output0Buffer.Column37 = columns.Length > 0 ? columns[37].ToString() : null; 
        Output0Buffer.Column38 = columns.Length > 0 ? columns[38].ToString() : null; 
        Output0Buffer.Column39 = columns.Length > 0 ? columns[39].ToString() : null; 
        Output0Buffer.Column40 = columns.Length > 0 ? columns[40].ToString() : null; 
        Output0Buffer.Column41 = columns.Length > 0 ? columns[41].ToString() : null; 
        Output0Buffer.Column42 = columns.Length > 0 ? columns[42].ToString() : null; 
        Output0Buffer.Column43 = columns.Length > 0 ? columns[43].ToString() : null; 
        Output0Buffer.Column44 = columns.Length > 0 ? columns[44].ToString() : null; 
        Output0Buffer.Column45 = columns.Length > 0 ? columns[45].ToString() : null; 
        Output0Buffer.Column46 = columns.Length > 0 ? columns[46].ToString() : null; 
        Output0Buffer.Column47 = columns.Length > 0 ? columns[47].ToString() : null; 
        Output0Buffer.Column48 = columns.Length > 0 ? columns[48].ToString() : null; 
        Output0Buffer.Column49 = columns.Length > 0 ? columns[49].ToString() : null; 

       } 
      } 
     } 
    } 

} 

請讓我知道,如果我需要從我的ssis更多的信息在這裏。謝謝!!

回答

1

SSIS方面並沒有改變任何與StreamReader讀取標題相關的內容,除非你想對標題行做一些有意義的事情。

只需撥打sr.ReadLine()您的循環之前(讀的第一行),什麼也不做它,就像這樣:

... 
sr.ReadLine(); 
while (!sr.EndOfStream) 
{ 
    string headline = sr.ReadLine(); 
    if (!string.IsNullOrEmpty(headline)) 
... 
+0

感謝@grovesNL。有效。 – Abhi

相關問題