簡體   English   中英

在SSIS腳本組件任務中讀取CSV文件

[英]Reading CSV file in SSIS Script Component task

我創建了一個轉換,以將數據從CSV文件復制到SQL Server表。 由於我的CSV文件是動態的,並且在給定時間可以具有任意數量的列,因此我包含了一個ScriptComponent Task來維護黑白“ FlatFileSource”“ OLEDBDestination”任務的映射。

我現在可以復制數據,但列名行也被添加為目標表中不需要的數據行。 以下是我在腳本任務中編寫的用於讀取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中獲取更多信息。 謝謝!!

SSIS方面實際上與StreamReader讀取標題無關,除非您想對標題行做一些有意義的事情。

只需在循環之前調用sr.ReadLine() (以讀取第一行),而對其不執行任何操作,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM