2011-11-14 40 views
1

我有一個SSIS包,可以根據一定的標準向用戶發送電子郵件,我現在想跟蹤以這種方式通知並向我們的團隊發送摘要的所有用戶。我是想保存這些變量中通知的列表(串)的usersname,但我似乎無法做到這一點的AA腳本任務,這裏是我的代碼:SSIS包:無法將System.Object轉換爲List(Of String)以存儲在Package變量中?

Public Sub Main() 
    Dim vars As Variables 
    If Dts.Variables("Users").Value Is Nothing Then 
     Dim users As New List(Of String) 
     users.Add("Enrollees: \n") 
     Dts.VariableDispenser.LockOneForWrite("Users", vars) 
     vars.Item("Users").Value = users 
    End If 

    'errors out on this line 
    Dim userList As List(Of String) = DirectCast(Dts.Variables("Users").Value, List(Of String)) 
    'errors out on this line 

    userList.Add(Dts.Variables("FirstName").Value.ToString() & " " & Dts.Variables("LastName").Value.ToString() & " (" & Dts.Variables("Email").Value.ToString() & ")\n") 
    Dts.VariableDispenser.LockOneForWrite("Users", vars) 
    vars.Item("Users").Value = userList 

    Dts.TaskResult = Dts.Results.Success 
End Sub 

變量用戶被設置爲Object類型,我知道你可以在那裏存儲複雜的類型,因爲我已經存儲了ADO.NET記錄集,但是我從來沒有通過腳本任務在代碼中完成這些工作。我也嘗試過CType,它給出了同樣的錯誤。我究竟做錯了什麼?

另外我知道我可以只存儲變量作爲逗號分隔在一個字符串中,並稍後分開,但我想知道爲什麼這不工作,並堅持更基於對象的方法。

謝謝。

+0

您收到了什麼具體錯誤? –

+0

無法將System.Object的對象轉換爲System.Collections.Generic.List [String] – SventoryMang

回答

2

問題是Users變量從未設置爲List(Of String) - 但它不是Nothing。相反,SSIS將其初始化爲System.Object,當然不能將轉換爲List(Of String)

Public Sub Main() 
    Dim msg As String 
    If Dts.Variables("Users").Value Is Nothing Then 
     msg = "Dts.Variables(""Users"").Value is Nothing" 
    Else 
     msg = "Dts.Variables(""Users"").Value is an object of type " + Dts.Variables("Users").Value.GetType().FullName 
    End If 
    Dts.Events.FireInformation(0, "Main", msg, "", 0, True) ' Information: Dts.Variables("Users").Value is an object of type System.Object 

    ' rest of your code follows 

幸運的是,所有你需要做的是在腳本任務初始化用戶到List(Of String)地方。

相關問題