繁体   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