通过检查条件加载 SSIS 平面文件 [英] SSIS flat file loading by checking conditions

查看:58
本文介绍了通过检查条件加载 SSIS 平面文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我在一个文件夹中有 10 个平面文件(.dat 格式),需要每天在预定时间上传到数据库中.
  • 所有文件相关信息都存在于数据库中,例如文件名、文件路径、表名、列名和分隔符.
  • 我们需要检查每个文件是否存在,如果不存在,需要记录一个条目,找不到文件".
  • 如果文件存在,则需要检查尾记录(文件中的最后一条记录,Count=00001000,它必须是该特定文件中的记录数).
  • 如果预告片记录不存在,则需要记录一个条目未找到预告片记录".如果预告片记录显示零计数,则必须将日志条目设为零计数",并且如果计数为文件不匹配需要一个日志条目,计数不匹配".
  • 如果满足所有条件,则需要将每个文件的数据加载到数据库中.

请提出您的想法来实现上述场景.谢谢!!!

Please suggest your ideas to implement the above scenario. Thanks!!!

推荐答案

以下解决方案可能会帮助您解决问题.

Following solution may help you to resolve the issue.

使用 For each 循环容器和 "Item" 枚举器.由于您有 10 个文件,如果缺少某些文件,您需要提高,那么您应该使用它.文件枚举器只是遍历文件,不会引发任何错误.

Use the For each loop container with "Item" enumerator. Since you have 10 files and if something missing you need raise then you should use this. File enumerator just iterate through the files, not raises any error.

以下是步骤.

使用变量创建以下 SSIS 包.

Create following SSIS package with variables.

  1. 文件完整路径
  2. 已验证

对于每个循环枚举器应按以下屏幕截图进行配置.

For each loop enumerator should be configured as following screenshots.

集合中的配置:

变量部分的配置

容器里面有一个脚本任务.你必须提到 FileFullPath 作为只读变量和 IsValidate 作为读写,如下面的屏幕.

Inside the container have a script task. you have to mention the FileFullPath as readonly variable and IsValidate as read and write like the following screen.

点击编辑脚本并插入以下代码.

Click Edit script and insert the following code.

public void Main()
{
        Dts.Variables["IsValidated"].Value = true;

        string fileFullPath = Dts.Variables["FileFullPath"].Value.ToString();

            if (!File.Exists(fileFullPath))
            {
                    var msg = String.Format("File is not available in location : {0}", fileFullPath);
                    Dts.Events.FireError(0, "Dat file loading", msg, string.Empty, 0);
                    Dts.TaskResult = (int)ScriptResults.Failure;
            }

            //Read last line
            String lstLine = File.ReadLines(fileFullPath).Last();

            int totalCount = 0;
            bool talierExists = int.TryParse(lstLine, out totalCount);

            if (!talierExists)
            {
                    var msg = String.Format("No tailer row found and last line is : {0}", lstLine);
                    Dts.Events.FireError(0, "Dat file loading", msg, string.Empty, 0);
                    Dts.TaskResult = (int)ScriptResults.Failure;
            }

            //Total count
            int fullCount = File.ReadLines(fileFullPath).Count();

            if (fullCount != totalCount)
            {
                    var msg = String.Format("No of count is not matching, tailer count = {0} and full count={1}");
                    Dts.Events.FireError(0, "Dat file loading", msg, string.Empty, 0);
                    Dts.TaskResult = (int)ScriptResults.Failure;
            }

            Dts.Variables["IsValidated"].Value = true;

            Dts.TaskResult = (int)ScriptResults.Success;
}

之后有你的数据流.将脚本任务与您的数据流连接起来,然后右键单击连接器,然后进行如下编辑和配置.

After that have your Data flow. Connect the script task with your data flow and right click on the connector and go to edit and configure as follows.

您的 SSIS 包将如下所示.

Your SSIS package will looks like follows.

希望这会有所帮助!

这篇关于通过检查条件加载 SSIS 平面文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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