SSIS脚本任务:从Array填充对象变量

时间:2014-06-01 15:38:45

标签: c# ssis

在SSIS中,我使用脚本任务为每个* .xml文件运行For Each循环 在我的文件夹中。所有.xml文件的名称都将传递给两个数组之一:arrayA和arrayB。

在脚本结束时,我试图为每个数组运行for循环,将每个存储的值添加到相关的对象变量objectA和objectB中。

从数组中填充对象变量的正确语法是什么?当我尝试在下面的每个循环中的脚本任务之外使用它们(处理每个文件)时,我得到一个错误:分配给变量的值的类型与当前变量类型

不同
    // For the sake of the question, it doesn't matter what A and B mean. I'm just                                 trying to show how the logic structured in a simplified way.

    public void Main()
    {
        // Reset Variable
        Dts.Variables["FileARecordCount"].Value = 0;

        string NotProcessedDirectory =          Dts.Variables["NotProcessedPath"].Value.ToString();
        string FileDirectory = Dts.Variables["FullPath"].Value.ToString();
        string[] files = Directory.GetFiles(FileDirectory, "*.xml");


        // Setting up our arrays which will be used to populate our object variables. 
        // Each is set to a size of 30, but this can be changed if needed.
        string[] FileAFileCollection = new string[30];
        string[] ShipmentInformationCollection = new string[30];
        int FileAIndex = 0;
        int InfoIndex = 0;


        // We're going to examine each xml file in our directory
        foreach (string file in files)
        {
            FileInfo CurrentFile = new FileInfo(file);

            // First, let's identify FileA files
            if (CurrentFile.LastWriteTime < DateTime.Now.AddMinutes(-10))
            {
                // Add each filename into an array which will populate our package object variable
                FileAFileCollection[FileAIndex] = CurrentFile.Name.ToString();
                FileAIndex++;


                // Before we move the file, let's check to see if the file exists already in the NotProcessed directory.
                if (File.Exists(NotProcessedDirectory + CurrentFile.Name))
                    File.Delete(NotProcessedDirectory + CurrentFile.Name);

                // Copy the file to the NotProcessed folder and delete the original
                CurrentFile.CopyTo(NotProcessedDirectory + CurrentFile.Name);
                CurrentFile.Delete();

                bool FileAMessage = false;
                Dts.Events.FireInformation(0, "FileA File Found", "File: " + CurrentFile.Name.ToString() + " moved to NotProcessed", string.Empty, 0, ref FileAMessage);
            }


            // If the file isn't an FileA, we want to get all Shipment Information files
            else
            {
                if (CurrentFile.Name.Substring(0, 6).ToString().ToUpper() == "FILEB")
                {


                    // Add each filename into an array which will populate our package object variable
                    ShipmentInformationCollection[InfoIndex] = CurrentFile.ToString();
                    InfoIndex++;
                }
            }
        } // End ForEach File


        // Add all of our FileA file names to our Ophan File object 
        if (FileAIndex > 0)
        {
            bool AddingFileAMessage = false;
            Dts.Events.FireInformation(0, "Adding FileA Files to Collection", FileAIndex + " file(s) added to collection", string.Empty, 0, ref AddingFileAMessage);
            Dts.Variables["FileARecordCount"].Value = FileAIndex;
            Dts.Variables["FileAFileCollection"].Value = FileAFileCollection;
        }

        // Add all of our Shipment Information file names to our Shipment Information Object 
        if (InfoIndex > 0)
        {

            Dts.Variables["ShipmentInformationCollection"].Value = ShipmentInformationCollection;
        }

    } //End Main

在此脚本任务结束后,我将使用一个使用ObjectVariableA作为其集合的ADO集合的每个容器,将所述变量的当前值传递给字符串变量FileName。为了澄清,我使用脚本任务将一堆文件名放入我的对象中,类型为&#34; A&#34;并循环遍历每个文件以继续我的逻辑。

非常感谢任何帮助。谢谢你的期待!

1 个答案:

答案 0 :(得分:3)

您似乎正在尝试在SSIS变量中添加/连接值。由于多种原因,这不会起作用。

第一个原因是,SSIS变量的数据类型大致类似于.NET原语。因此,+=不会做你认为它会做的事情(假设它不会彻底爆炸)。

第二个原因是您正在对基础对象本身进行操作。相反,您可能希望分配.Value属性。这就是在ForEach循环结构中自动访问的内容。

// illogical for SSIS
for(int i = 0; i < fileAIndex; i++)
    Dts.Variables["ObjectVariableA"] += fileA[i].toString();
// Replaced with
Dts.Variables["ObjectVariableA"].Value = fileA;

只需将像对象一样的数组分配给Object类型的SSIS变量。 Object可以保存从object派生的任何内容。拥有一个DataSet,将其分配给一个Object。你自己的自定义类,相同。

这解决了问题的技术部分。我强烈建议如果你能解释你在做什么,我们可以找到更多的SSIS-ic(并不会产生与 pythonic 相同的影响)的做法的东西。