SSIS 将平面文件导入 SQL,第一行为 header,最后一行为总计

SSIS import a Flat File to SQL with the first row as header and last row as a total

我收到必须导入到 SQL Table 的文本文件,我必须使用 SSIS,因为我每天都会收到平面文件,第一行是Customer_ID,然后是发票详细信息,然后是发票总额。

示例:

30303

0000109291700080190432737000005产品名称

0000210291700080190432737000010产品名称

0000309291700080190432737000000产品名称

003 000145

所以让我解释一下:

第一个30303是客户#

其他行发票明细

00001->ROWID 092917->日期 000801904327->产品 370->交易 00010->金额
产品名称

最后一行

003==>总行数 000145==>发票总数

有什么线索吗?

我会使用脚本组件作为数据流任务中的源。然后,您可以使用 C# 或 VB.net 以任何您希望的方式读取文件,例如,通过使用 System.IO.StreamReader。您可以一次读取一行,将值存储在变量中以写入每一行(例如,客户编号)等。它对于复杂文件非常灵活。

这是一个基于您的数据的示例脚本 (C#):

public override void CreateNewOutputRows()
{
    System.IO.StreamReader reader = null;

    try
    {
        bool line1Read = false;
        int customerNumber = 0;

        reader = new System.IO.StreamReader(Variables.FilePath); // this refers to a package variable that contains the file path

        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();

            if (!line1Read)
            {
                customerNumber = Convert.ToInt32(line);
                line1Read = true;
            }
            else if (!reader.EndOfStream)
            {
                Output0Buffer.AddRow();

                Output0Buffer.CustomerNumber = customerNumber;
                Output0Buffer.RowID = Convert.ToInt32(line.Substring(0, 5));
                Output0Buffer.Date = DateTime.ParseExact(line.Substring(5, 6), "MMddyy", System.Globalization.CultureInfo.CurrentCulture);
                Output0Buffer.Prod = line.Substring(11, 12);
                Output0Buffer.Trans = Convert.ToInt32(line.Substring(23, 3));
                Output0Buffer.Amount = Convert.ToInt32(line.Substring(26, 5));
                Output0Buffer.ProductName = line.Substring(31);
            }
        }
    }
    catch
    {
        if (reader != null)
        {
            reader.Close();
            reader.Dispose();
        }

        throw;
    }
}

脚本组件'Output 0'中的列配置如下:

Name             DataType                           Length
====             ========                           ======
CustomerNumber   four-byte signed integer [DT_I4]
RowID            four-byte signed integer [DT_I4]
Date             database date [DT_DBDATE]
Prod             string [DT_STR]                        12
Trans            four-byte signed integer [DT_I4]
Amount           four-byte signed integer [DT_I4]
ProductName      string [DT_STR]                       255

实现这个:

  • 创建一个名为 'FilePath' 的字符串变量,其中包含您的文件路径以供脚本引用。
  • 创建数据流任务。
  • 将脚本组件添加到数据流任务 - 您将被问到它应该是什么类型,select 'Source'.
  • 右键单击脚本组件,单击'Edit'。
  • 在 'Script' 窗格中,将 'FilePath' 变量添加到 'ReadOnlyVariables' 部分。
  • 在 'Inputs and Outputs' 窗格中,展开 'Output 0' 并按照上述 table.
  • 将列添加到 'Output Columns' 部分
  • 在 'Script' 窗格中,单击 'Edit Script',然后将我的代码粘贴到 public override void CreateNewOutputRows() 方法(替换它)上。
  • 您的脚本组件源现已配置完毕,您可以像使用任何其他数据源组件一样使用它。要将此数据写入 SQL 服务器 table,请将 OLEDB 目标添加到数据流任务,然后将脚本组件 link 添加到该目标,适当配置列。