SSIS For循环停止工作 [英] SSIS For Loop Stopped working

查看:97
本文介绍了SSIS For循环停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的sis软件包中有一个for循环容器,其中包含一个脚本和一个sql任务.

I have a for loop container within my ssis package which contains a script and a sql task.

我有3个变量.

source.string = this is folder location 
file.string = i have used wildcard = *.csv
exist.int = defaulted to 0

我将innitexpression值设置为@ Exists = 1 并将评估值设置为@ Exists = 1

I have the innitexpression value set to @Exists=1 and the evalexpression value set to @Exists=1

在脚本中,我将其设置为查看源变量,如果存在file.string变量,则将exist变量设置为1

in the script I have set it to look at source variable and if file.string variable exists then set exist variable to 1

问题是它只是循环而已,如果那里没有文件,就应该循环.在将变量更改为通配符* .csv

problem is it just loops it should only loop if no file there. cant see how I've done this wrong it was working before I changed the variable to be a wildcard *.csv

我已经使用另一个包含文件名而不是通配符的变量对其进行了测试,它可以正常工作,问题是在查找文件名后跟扩展名的通配符时.为什么是这样?我不能通过通配符变量吗?

I have tested it using another variable which contains a filename rather than a wildcard and it works correctly the issue is when looking for a wildcard for the filename followed by the extension. why is this? can I not pass through a wildcard variable?

我的脚本任务是

      public void Main()
        {
            // TODO: Add your code here
            string Filepath = Dts.Variables["User::Source"].Value.ToString() 
+ Dts.Variables["User::file"].Value.ToString();
            if (
                File.Exists(Filepath))
            {
                Dts.Variables["User::Exists"].Value = 1;
            }

            /// MessageBox.Show (Filepath);
            /// MessageBox.Show(Dts.Variables["Exists"].Value.ToString());
            Dts.TaskResult = (int)ScriptResults.Success;
        }

        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

    }
}

推荐答案

基于上面的评论,我提出了两种不同的解决方案.现在为您提供的解决方案是否. 2

Based on comments above i made 2 different solutions. The solution for you right now would be no. 2

  1. 此文件可以根据路径中的多个文件搜索特定文件.它需要一些调整,但是如果您想检查通配符是否存在特定文件,可以使用

  1. This one can search for a specific file based on multiple files in your path. It need some tweaking but can be used if you wanna check if a specific file exists with wildcard

如果找到任何通配符文件,则此评估为true.

This one evaluates to true if any wildcard file is found.

C#代码1

Using System.IO:

string Filepath = Dts.Variables["User::Source"].Value.ToString();
            string WildCard = Dts.Variables["User::file"].Value.ToString(); // In Text form @"*.txt";
            string fullpath = Filepath + WildCard;

            //With for loop
            string txtFile = null;
            // Gets all files with wildcard
            string[] allfiles = Directory.GetFiles(Filepath, WildCard);
            
            //Loop through all files and set the filename in txtFile. Do whatever you want here
            foreach(string fileName in allfiles)
            {
                //Check if a file contains something, it could be a prefixed name you only want
                if(fileName.Contains("txt"))
                {
                    txtFile = fileName;
                    if(File.Exists(txtFile))
                    {
                        Dts.Variables["User::Exists"].Value = 1;
                    }
                }
            }

C#代码2

 Using System.IO;
 Using System.Linq;

 string Filepath = Dts.Variables["User::Source"].Value.ToString();
            string WildCard = Dts.Variables["User::file"].Value.ToString(); //In text form "*.txt";
            string fullpath = Filepath + WildCard;

            //With bool
            bool exists = Directory.EnumerateFiles(Filepath, WildCard).Any();

            if(exists == true)
            {
                Dts.Variables["User::Exists"].Value = 1;
            }

              
            MessageBox.Show (Filepath);
            MessageBox.Show(Dts.Variables["Exists"].Value.ToString());

这篇关于SSIS For循环停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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