Can a array list of C# be used to populate SSIS ob

2020-07-22 09:11发布

I have populated a list in C# script and assigned its value to SSIS object variable.

Then I used that object variable to execute some SQL query by looping through For each do enumerator.

I tried doing this by Foreach ado enumerator but getting error

X variable doesn't contain a valid data object.

Can anybody provide any inputs.

enter image description here

2条回答
我欲成王,谁敢阻挡
2楼-- · 2020-07-22 09:34

Youre using a list. Not a recordset and therefore you need to enumerate over a variable.

If you want to use ADO Recordset, you need to fill a datatable instead.

  1. This shows you how to write to object with a variable list

  2. This shows you how to write to object with recordset (using multiple values)

Like this:

1 .C# Script code - Write to Object with list using variable enumerator

public void Main()
    {
        // TODO: Add your code here

        List<string> NewList = new List<string>();

        NewList.Add("Ost");
        NewList.Add("Hest");

        Dts.Variables["User::NameList"].Value = NewList;

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

1. Variable settings in ssis enter image description here

1. Foreach loop container settings

Use Foreach Variable Enumerator and use your object variable

enter image description here

Map your outcome to a variable(s)

enter image description here

1. Execute SQL Task test case

Write your SQL with variables

enter image description here

Map your variable to Parameter mapping

enter image description here

1. Result

enter image description here

2. C# Script code - Write to object with datatable using ADO enumerator

   public void Main()
    {
        // TODO: Add your code here

        DataTable dt = new DataTable();

        dt.Columns.Add("FilmName",typeof(string));
        dt.Columns.Add("ActorName",typeof(string));

        dt.Rows.Add("Starwars", "Harrison ford");
        dt.Rows.Add("Pulp fiction", "Samuel Jackson");


        Dts.Variables["User::NameList"].Value = dt;

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

2. Variable settings in ssis

enter image description here

2. Foreach loop container settings

Use Foreach ADO Enumerator and your object as variable

enter image description here

Map your outcome to variable(s)

enter image description here

2. Execute sql task test case

Write your SQL with variables

enter image description here

Map your variable(s) to Parameter mapping

enter image description here

2. Result

enter image description here

查看更多
神经病院院长
3楼-- · 2020-07-22 09:49

Thanks @plaidDK Second approch solved my problem

2.C# Script code - Write to object with datatable using ADO enumerator

Instead of list I have populated data table:

public DataTable ToDataTable<T>(List<T> items)
{
    DataTable dataTable = new DataTable(typeof(T).Name);
    //Get all the properties by using reflection   
    PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (PropertyInfo prop in Props)
    {
        //Setting column names as Property names  
        dataTable.Columns.Add(prop.Name);
    }
    foreach (T item in items)
    {
        var values = new object[Props.Length];
        for (int i = 0; i < Props.Length; i++)
        {

            values[i] = Props[i].GetValue(item, null);
        }
        dataTable.Rows.Add(values);
    }

    return dataTable;
}  

//Variable passed as below Variables.vFailedTransactionNo = dt;

ANd then ado enumerator done rest of the job.

Thanks for help!

查看更多
登录 后发表回答