Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据流脚本组件中的记录集枚举为数据源_C#_Ssis - Fatal编程技术网

C# 将数据流脚本组件中的记录集枚举为数据源

C# 将数据流脚本组件中的记录集枚举为数据源,c#,ssis,C#,Ssis,这是一个与SSIS相关的问题 我有一个变量设置为type object。一个数据流将一些过滤后的行导入记录集中,该记录集存储在对象变量中 在一个完全独立的数据流中,我需要使用该记录集作为源。所以我创建了一个脚本组件,并告诉它将是一个数据源 我将其设置为具有所需的三个输出列。我的问题是,如何让记录集中的每一行在脚本组件中创建新行 我将记录集变量作为只读变量传入,当我尝试foreach该变量以到达每一行时,我无法这样做,因为该变量没有定义get枚举器方法 因此,我无法将每一行打印到这些列中,也无法将

这是一个与SSIS相关的问题

我有一个变量设置为type object。一个数据流将一些过滤后的行导入记录集中,该记录集存储在对象变量中

在一个完全独立的数据流中,我需要使用该记录集作为源。所以我创建了一个脚本组件,并告诉它将是一个数据源

我将其设置为具有所需的三个输出列。我的问题是,如何让记录集中的每一行在脚本组件中创建新行

我将记录集变量作为只读变量传入,当我尝试foreach该变量以到达每一行时,我无法这样做,因为该变量没有定义get枚举器方法

因此,我无法将每一行打印到这些列中,也无法将脚本组件用作数据源

还有其他人面临过类似的情况吗?我是在做傻事还是你用另一种方式做的


注意,我在脚本和VisualStudio2008中使用了C#

我在旧版本的基础上做了类似的事情。我们的子包填充一个记录集,指示我们有多少新的、更改的、未更改的等行计数。在父包中,我们测试该对象,以确保在使用它之前已填充该对象。我需要跳到一个会议,所以请原谅这段代码,因为它不能完全解决您的具体需求,但希望它能在正确的方向上提供坚实的推动力,直到我可以回来为您的情况量身定做。我有伪代码在那里,你会想做的东西

    public void Main()
    {
        bool debug = Convert.ToBoolean(Dts.Variables["Debug"].Value);
        string taskName = string.Empty;
        string packageName = string.Empty;
        string sourceName = string.Empty;
        bool fireAgain = false;

        taskName = Convert.ToString(Dts.Variables["TaskName"].Value);
        packageName = Convert.ToString(Dts.Variables["PackageName"].Value);
        // Fix this by defining and passing in params
        sourceName = Convert.ToString(Dts.Variables["TaskName"].Value);

        System.Data.OleDb.OleDbDataAdapter adapater = null;
        System.Data.DataTable table = null;
        System.Data.DataColumn column = null;
        System.Data.DataRow row = null;
        string message = string.Empty;

        object rowCounts = null;
        rowCounts = Dts.Variables["RowCounts"].Value;
        table = new DataTable();

        try
        {
            // Get us out of this crazy thing - should only be an issue
            // first pass through
            if (rowCounts == null)
            {
                Dts.TaskResult = (int)ScriptResults.Success;
                return;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Failed here");
        }

        adapater = new System.Data.OleDb.OleDbDataAdapter();
        try
        {
            // This works if we pass in a dataset
            //adapater.Fill(table, Dts.Variables["RowCounts"].Value);
            adapater.Fill(table, rowCounts);
            // TODO: Enumerate through adapter
            // Call Output0Buffer.AddRow();
            // and Output0Buffer.MyColumn.Value = adapter[i].value // possibly casting to strong type
        }
        catch (Exception ex)
        {
            try
            {
                // This works if we use a datatable
                System.Data.DataSet ds = null;
                //ds = (DataSet)Dts.Variables["RowCounts"].Value;
                ds = (DataSet)rowCounts;
                table = ds.Tables[0];
                // TODO: Enumerate through datatable as we do with adapter

            }
            catch (Exception innerException)
            {
                // continue to swallow exceptions
            }
            Dts.Variables["ValidCounts"].Value = false;

            // trap "Object is not an ADODB.RecordSet or an ADODB.Record
            // parse ex.Message
            if (ex.Message.Contains("System.ArgumentException: "))
            {
                System.Text.StringBuilder exceptionMessage = null;
                exceptionMessage = new System.Text.StringBuilder();
                exceptionMessage.Append(ex.Message);
                exceptionMessage.Replace("\nParameter name: adodb", string.Empty);
                exceptionMessage.Replace("System.ArgumentException: ", string.Empty);

                if (exceptionMessage.ToString() != "Object is not an ADODB.RecordSet")
                {
                    Dts.Events.FireInformation(0, string.Format("{0}.{1}", packageName, taskName), exceptionMessage.ToString(), string.Empty, 0, ref fireAgain);
                }
            }
        }

        Dts.Variables["ValidCounts"].Value = false;
        if (table.Rows.Count > 0)
        {
            Dts.Variables["ValidCounts"].Value = true;
        }

        if (debug)
        {
            message = string.Format("SourceName:  {0}\nValidCounts:  {1}", sourceName, false);
            //System.Windows.Forms.MessageBox msgBox = null;
            //msgBox = new MessageBox();
            System.Windows.Forms.MessageBox.Show(message, string.Format("{0}.{1}", packageName, taskName));
            //MessageBox(message, string.Format("{0}.{1}", packageName, taskName));
        }

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

因此,我环顾四周,找到了一个解决我的问题的VB解决方案,我把它翻译成C#,现在它的编译和行为与预期的一样。我使用的代码是:

    DataTable datatable = new DataTable();
    System.Data.OleDb.OleDbDataAdapter oAdapter = new System.Data.OleDb.OleDbDataAdapter();

    oAdapter.Fill(datatable,ReadOnlyVariables["User::XXXXX"]);

    foreach (DataRow row in datatable.Rows)
    {
        Output0Buffer.AddRow();
        Output0Buffer.CoverAmount = Convert.ToInt32(row["XXXX"].ToString());
    } 
对于任何其他面临类似问题的人


感谢所有人的帮助

我正在处理与OP相同的问题。在我的例子中,我假设它是一个对象,我花了很多时间试图将其转换为数据表。我发现该对象已经是一个datatable,因此通过SSIS脚本任务组件,我可以编写以下内容:

DataTable dt = (DataTable)ReadOnlyVariables["User::FTP_DataPath_File_Metadata"].Value;

foreach (DataRow row in dt.Rows)
{
    CustomOutputBuffer.AddRow();
    CustomOutputBuffer.FileName = row.ItemArray[0].ToString();
    CustomOutputBuffer.FileLastModified = Convert.ToDateTime(row.ItemArray[1]);
    CustomOutputBuffer.FileSize = Convert.ToInt32(row.ItemArray[2]);
}
这将使用脚本组件作为源成功地将我的“对象变量”转换为数据流


此示例用于容纳Task Factory Secure FTP组件,该组件将“Get file list with metadata”的结果保存到对象变量中

您是否使用了ADO ForEach来枚举对象?这给了我一个错误,
无法将'System.\u ComObject'类型的COM对象强制转换为'System.Data.DataTable'类型的类。