SSIS脚本任务:从数组中填充对象变量

本文关键字:填充 对象 变量 数组 脚本 任务 SSIS | 更新日期: 2023-09-27 18:06:19

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

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

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

    // 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

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

非常感谢任何帮助。感谢您的关注!

SSIS脚本任务:从数组中填充对象变量

您似乎正在尝试在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赋值给object类型的SSIS变量。对象可以保存从object派生的任何东西。有一个数据集,将它分配给一个对象。您自己的自定义类,相同。

这解决了你问题的技术部分。我强烈建议如果你能解释一下你在做什么,我们可以找到一种更siss -ic(不太像python的)的做事方式。