在SSIS 2016中导入具有修订长度列的txt文件时验证行长度

时间:2018-04-30 15:18:41

标签: text import ssis

我有一个固定长度的文件,我必须阅读并验证。该文件由另一个系统生成,但有时员工正在对其进行手动更改。例如:

布局

变量:姓氏大小:30 1 -30
变量:名称大小:30 31-60 变量:电子邮件大小:30 61-90 变量:评论大小:30 91-120 变量:CarriageReturn大小:2 121-123

因此系统会生成以下文本文件: Source file

但是然后有一个人工干预,该人不尊重列长: Source file after manual intervention

因此,在开始验证列中的值之前,所有内容都会被抵消,因为我的第一个回车现在正在拆分我的"评论"我在SSIS中读到它时的列。

有没有办法告诉系统,如果长度行超过2033,输出错误文件并继续?做这个的最好方式是什么?

Mylene的

1 个答案:

答案 0 :(得分:0)

我找到了!!

//Pass the file path and file name to the StreamReader and StreamWriter constructors
            StreamReader sr = new StreamReader(inputFile);
            StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString);
            StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString);

            //Read the first line
            line = sr.ReadLine();

            while (line != null)
            {
               int length = line.Length;
               if (length > 2033)
               {
                    if 
                    {
                        swe.WriteLine("Some records have been rejected at the pre validation phase.");
                        swe.WriteLine("Those records will not be included in the process.");
                        swe.WriteLine("Please review the records below, fix and re submit if applicable.");
                        swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
                        swe.WriteLine();
                        swe.WriteLine(line);
                        count++;
                    }
                    else
                    {
                        swe.WriteLine(line);
                        count++;
                    }
               }
               if (length <= 2033)
               {
                    sw.WriteLine(line);
               }
                line = sr.ReadLine(); 
            }