您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

SSIS将平面文件导入到SQL,其中第一行为标头,最后一行为总数

SSIS将平面文件导入到SQL,其中第一行为标头,最后一行为总数

我将使用脚本组件作为数据流任务中的源。然后,您可以使用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;
    }
}

脚本组件的“输出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

要实现这一点:

SQLServer 2022/1/1 18:50:24 有444人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶