简体   繁体   English

导入带有动态列的文件

[英]Importing a File with Dynamic Columns

I am new to SSIS and C#. 我是SSIS和C#的新手。 In SQL Server 2008 I am importing data from a .csv file. 在SQL Server 2008中,我正在从.csv文件导入数据。 Now I have the columns dynamic. 现在,我有了动态的列。 They can be around 22 columns(some times more or less). 它们可以在22列左右(有时或多或少)。 I created a staging table with 25 columns and import data into it. 我创建了一个具有25列的登台表,并将数据导入其中。 In essence each flat file that I import has different number of columns. 本质上,我导入的每个平面文件都有不同的列数。 They are all properly formatted only. 它们都仅被正确格式化。 My task is to import all the rows from a .csv flat file including the headers. 我的任务是从包括标题的.csv平面文件中导入所有行。 I want to put this in a job so I can import multiple files into the table daily. 我想把它放在工作中,这样我就可以每天将多个文件导入表中。

So inside a for each loop I have a data flow task within which I have a script component. 因此,对于每个循环,我都有一个数据流任务,其中有一个脚本组件。 I came up(research online) with the C# code below but I get error: 我想出了下面的C#代码(在线研究),但出现错误:

Index was outside the bounds of the array. 指数数组的边界之外。

I tried to find the cause using MessageBox and I found it is reading the first line and the index is going outside the bounds of the array after the first line. 我尝试使用MessageBox查找原因,发现它正在读取第一行,并且索引在第一行之后超出了数组的范围。

1.) I need your help with fixing the code 1.)我需要您的帮助来修复代码

2.) My File1Conn is the flat file connection instead I want to read it directly from a variable User::FileName that my foreach loop keeps updating. 2.)我的File1Conn是平面文件连接,相反,我想直接从我的foreach循环不断更新的User :: FileName变量中读取它。 Please help with modifying the code below. 请帮助修改以下代码。

Thanks in advance. 提前致谢。

This is my flat file: 这是我的平面文件:

https://drive.google.com/file/d/0B418ObdiVnEIRnlsZFdwYTRfTFU/view?usp=sharing https://drive.google.com/file/d/0B418ObdiVnEIRnlsZFdwYTRfTFU/view?usp=sharing

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Windows.Forms;
using System.IO;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent

{
 private StreamReader SR;
 private string File1;

public override void AcquireConnections(object Transaction)
{
    // Get the connection for File1
    IDTSConnectionManager100 CM = this.Connections.File1Conn;
    File1 = (string)CM.AcquireConnection(null);
}

public override void PreExecute()
{
    base.PreExecute();
    SR = new StreamReader(File1);
}

public override void PostExecute()
{
    base.PostExecute();
    SR.Close();
}

public override void CreateNewOutputRows()
{
    // Declare variables
    string nextLine;
    string[] columns;
    char[] delimiters;
    int Col4Count;
    String[] Col4Value = new string[50];

    // Set the delimiter
    delimiters = ";".ToCharArray();

    // Read the first line (header)
    nextLine = SR.ReadLine();

    // Split the line into columns
    columns = nextLine.Split(delimiters);

    // Find out how many Col3 there are in the file
    Col4Count = columns.Length - 3;
    //MessageBox.Show(Col4Count.ToString());

    // Read the second line and loop until the end of the file
    nextLine = SR.ReadLine();

    while (nextLine != null)
    {

        // Split the line into columns
        columns = nextLine.Split(delimiters);
        {
            // Add a row
            File1OutputBuffer.AddRow();


            // Set the values of the Script Component output according to the file content
            File1OutputBuffer.SampleID = columns[0];
            File1OutputBuffer.RepNumber = columns[1];
            File1OutputBuffer.Product = columns[2];
            File1OutputBuffer.Col1 = columns[3];
            File1OutputBuffer.Col2 = columns[4];
            File1OutputBuffer.Col3 = columns[5];
            File1OutputBuffer.Col4 = columns[6];
            File1OutputBuffer.Col5 = columns[7];
            File1OutputBuffer.Col6 = columns[8];
            File1OutputBuffer.Col7 = columns[9];
            File1OutputBuffer.Col8 = columns[10];
            File1OutputBuffer.Col9 = columns[11];
            File1OutputBuffer.Col10 = columns[12];
            File1OutputBuffer.Col11 = columns[13];
            File1OutputBuffer.Col12 = columns[14];
            File1OutputBuffer.Col13 = columns[15];
            File1OutputBuffer.Col14 = columns[16];
            File1OutputBuffer.Col15 = columns[17];
            File1OutputBuffer.Col16 = columns[18];


        }

        // Read the next line
        nextLine = SR.ReadLine();

    }
}

}

As you mentioned the file has dynamic amount of columns, in your script component you need to count number of columns by delimiters, then redirect to different outputs. 正如您提到的,文件具有动态的列数,在脚本组件中,您需要通过定界符计算列数,然后重定向到不同的输出。

For your 2nd question, you can assign your variable to the flat file connection manager connection string property. 对于第二个问题,您可以将变量分配给平面文件连接管理器连接字符串属性。 Then you can read the variable value in your script directly. 然后,您可以直接在脚本中读取变量值。

Except for script component, you can create a "one column" flat file source by using a dummy delimiter, then in the data flow task, you can read amount of columns into a variable, conditional split the data flow, redirect the outputs into different destinations. 除了脚本组件,您可以使用虚拟定界符创建“一列”平面文件源,然后在数据流任务中,您可以将列数读取到变量中,有条件地拆分数据流,将输出重定向到不同的位置目的地。 An example can be found at http://sqlcodespace.blogspot.com.au/2015/03/ssis-design-pattern-handling-flat-file.html 可以在http://sqlcodespace.blogspot.com.au/2015/03/ssis-design-pattern-handling-flat-file.html上找到示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM