【问题标题】:Convert package variable of type System.Object to a List<String> with SSIS C# Script task使用 SSIS C# 脚本任务将 System.Object 类型的包变量转换为 List<String>
【发布时间】:2019-02-08 12:43:21
【问题描述】:

我正在使用以下代码:

Object duplicateDevices = Dts.Variables["User::DuplicateDevices"].Value;
        List<DuplicateDeviceModel> lstduplicateDevices = (List<DuplicateDeviceModel>)duplicateDevices;

User::DuplicateDevices 被声明为变量中的对象,但在脚本任务中,同时类型转换我得到类型转换错误;我希望它被转换成List&lt;DuplicateDeviceModel&gt;

【问题讨论】:

  • 变量是否在进入此脚本前初始化?

标签: c# ssis casting etl script-task


【解决方案1】:

尝试使用以下语法转换为IEnumerable:

IEnumerable lstduplicateDevices = duplicateDevices as IEnumerable;

如果有效,您可以使用以下语法指定 `:

IEnumerable<DuplicateDeviceModel> ieDDM = lstduplicateDevices.OfType<DuplicateDeviceModel>();

参考

【讨论】:

    【解决方案2】:

    在脚本中,首先尝试从DuplicateDevices 对象变量填充数据表。在此之后,使用数据表填充类型为 DuplicateDeviceModel 的列表。在不知道您正在使用的列或数据类型的情况下,以下是一个一般示例,列及其数据类型需要根据您正在使用的那些进行调整。

    using System.Collections.Generic;
    using System.Data.OleDb;
    
    
            List<DuplicateDeviceModel> lstduplicateDevices = new List<DuplicateDeviceModel>();
    
            DataTable dt = new DataTable();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.Fill(dt, Dts.Variables["User::DuplicateDevices"].Value);
    
            foreach (DataRow dr in dt.Rows)
               {
                //convert columns from data type to data types of list as necessary
                lstduplicateDevices.Add(new DuplicateDeviceModel() { 
                ColumnA = Convert.ToInt32(dr[0]),
                ColumnB = dr[1].ToString() });
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2013-09-13
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多