开发者

SSIS - How to access a RecordSet variable inside a Script Task

开发者 https://www.devze.com 2022-12-26 16:04 出处:网络
How do you access a Rec开发者_如何转开发ordSet variable inside a Script Task?Listed below is the code I used to load a datatable in a C# script task from a recordset or resultset variable. \"User::tra

How do you access a Rec开发者_如何转开发ordSet variable inside a Script Task?


Listed below is the code I used to load a datatable in a C# script task from a recordset or resultset variable. "User::transactionalRepDBs" is a SSIS variable of Object (System.Object) that was loaded through a "Full result set" from a execute SQL task script. This link assisted me.

using System.Data.OleDb;

DataTable dt= new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.Fill(dt, Dts.Variables["User::transactionalRepDBs"].Value);

foreach (DataRow row in dt.Rows)
{
   //insert what you want to do here
}


On the script tab, make sure you put the variable in either the readonlyvariables or readwritevariables text boxes.

Here is a simple script that I use to format the errors in a data flow (saved in a RecordSet Variable) into the body of an email. Basically I read the recordset varialbe into a datatable and process it row by row with the for loops. After this task completes I examine the value of uvErrorEmailNeeded to determine if there is anything to email using a conditional process flow connector. You will also need to add a reference to system.xml in your vb script. This is in SQL 2005.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Xml
Imports System.Data.OleDb


Public Class ScriptMain


    Public Sub Main()


        Dim oleDA As New OleDbDataAdapter
        Dim dt As New DataTable
        Dim col As DataColumn
        Dim row As DataRow
        Dim sMsg As String
        Dim sHeader As String


        oleDA.Fill(dt, Dts.Variables("uvErrorTable").Value)
        If dt.Rows.Count > 0 Then
            Dts.Variables("uvErrorEmailNeeded").Value = True
            For Each col In dt.Columns
                sHeader = sHeader & col.ColumnName & vbTab
            Next
            sHeader = sHeader & vbCrLf
            For Each row In dt.Rows
                For Each col In dt.Columns
                    sMsg = sMsg & row(col.Ordinal).ToString & vbTab
                Next
                sMsg = sMsg & vbCrLf
            Next
            Dts.Variables("uvMessageBody").Value = "Error task. Error list follows:" & vbCrLf & sHeader & sMsg & vbCrLf & vbCrLf
        End If

        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class


An easier way I found (using C#) is simply casting the object as a string array. This is how my C# code looks now:

public void Main()
{
 string[] arreglo = (string[])Dts.Variables["User::arreglo"].Value;

 ...

 foreach (string elemento in arreglo)
 {
  // do stuff on each element of the array/collection
 }

 ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号