SSIS 将平面文件导入 SQL,第一行作为标题,最后一行作为总数 [英] SSIS import a Flat File to SQL with the first row as header and last row as a total

查看:34
本文介绍了SSIS 将平面文件导入 SQL,第一行作为标题,最后一行作为总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I receive Text File that I have to Import to a SQL Table, I have to come with a SSIS because I will received the Flat File every Day , with the First Row as the Customer_ID, then come the invoice details and then the Total of the invoice.

示例:

30303

0000109291700080190432737000005产品名称

0000109291700080190432737000005Name of the product

0000210291700080190432737000010产品名称

0000210291700080190432737000010Name of the product

0000309291700080190432737000000产品名称

0000309291700080190432737000000Name of the product

003 000145

让我解释一下:

第一个 30303 是客户编号

First 30303 is the Customer #

其他行发票详细信息

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

00001-> ROWID 092917-> DATE 000801904327->PROD 370->Trans 00010 -> AMOUNT
Name of the product

最后一行

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

003==>Total rows 000145==>Total of Invoice

有什么线索吗?

推荐答案

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

I would use a Script Component as a source in a Data Flow Task. You can then use C# or VB.net to read the file, e.g., by using System.IO.StreamReader, in any way you wish. You can read a line at a time, store values in variables to write to every row (e.g., the customer number), etc. It's extremely flexible for complex files.

以下是基于您的数据的示例脚本 (C#):

Here is an example script (C#) based on your data:

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'中的列配置如下:

The columns in 'Output 0' of the Script Component are configured as follows:

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"的字符串变量,其中包含您的文件路径以供脚本引用.
  • 创建数据流任务.
  • 向数据流任务添加脚本组件 - 系统会询问您应该是什么类型,选择源".
  • 右键单击脚本组件,然后单击编辑".
  • 在脚本"窗格中,将FilePath"变量添加到ReadOnlyVariables"部分.
  • 在输入和输出"窗格中,展开输出 0"并按照上表将列添加到输出列"部分.
  • 在脚本"窗格中,单击编辑脚本",然后将我的代码粘贴到 public override void CreateNewOutputRows() 方法上(替换它).
  • 您的脚本组件源现已配置完毕,您将能够像使用任何其他数据源组件一样使用它.要将这些数据写入 SQL Server 表,请将 OLEDB 目标添加到数据流任务,并将脚本组件链接到该目标,适当地配置列.

这篇关于SSIS 将平面文件导入 SQL,第一行作为标题,最后一行作为总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆