脚本任务错误 - 无法将 GetListings 文件列表放入对象变量中

发布于 2024-12-17 08:29:39 字数 1419 浏览 0 评论 0原文

使用下面的脚本任务将文件列表填充到 User::ftp_file_list 中。但是,下一步中的 For Each 循环容器会返回错误“Foreach 循环容器:变量“User::ftp_file_list”中的对象不包含枚举器”。

好的,事实证明问题是未建立连接。我不明白的是,我已经独立测试了连接,没有任何问题。所以,我有点困惑为什么它没有在脚本任务中连接。

有人有什么想法吗?提前致谢!

Public Sub Main()
    Dim result As Integer
    Dim manager As ConnectionManager
    Dim ftpClient As FtpClientConnection
    Dim foldersList As String()
    Dim filesList As String()
    Dim var As Variables

    manager = Dts.Connections("FTP_Connection")

    ftpClient = New FtpClientConnection(manager.AcquireConnection(Nothing))

    Try
        If ftpClient.Connect() Then

            Call ftpClient.SetWorkingDirectory(Dts.Variables("ftp_path").ToString)
            Call ftpClient.GetListing(foldersList, filesList)


            ' Store files list in package variable.'
            Call Dts.VariableDispenser.LockOneForWrite("ftp_file_list", var)

            Try
                var("ftp_file_list").Value = filesList
            Finally
                Call var.Unlock()
            End Try

        End If

    Catch ex As Exception

        result = Dts.TaskResult = ScriptResults.Failure

        Call Dts.Events.FireError( _
            0, _
            String.Empty, _
            ex.Message, _
            String.Empty, _
            0)

    Finally

        Call ftpClient.Close()

    End Try

    Dts.TaskResult = ScriptResults.Success

End Sub ' Main

Using the script task below to stuff list of files into User::ftp_file_list. But, the For Each Loop container in the next step returns the error "Foreach Loop Container: The object in the variable "User::ftp_file_list" does not contain an enumerator."

OK, turns out that the issue is that the connection is not being made. The thing I don't understand is that I've tested the connection independently without any trouble. So, I'm a bit perplexed why it's not connecting in the script task.

Anyone have any thoughts? Thanks in advance!

Public Sub Main()
    Dim result As Integer
    Dim manager As ConnectionManager
    Dim ftpClient As FtpClientConnection
    Dim foldersList As String()
    Dim filesList As String()
    Dim var As Variables

    manager = Dts.Connections("FTP_Connection")

    ftpClient = New FtpClientConnection(manager.AcquireConnection(Nothing))

    Try
        If ftpClient.Connect() Then

            Call ftpClient.SetWorkingDirectory(Dts.Variables("ftp_path").ToString)
            Call ftpClient.GetListing(foldersList, filesList)


            ' Store files list in package variable.'
            Call Dts.VariableDispenser.LockOneForWrite("ftp_file_list", var)

            Try
                var("ftp_file_list").Value = filesList
            Finally
                Call var.Unlock()
            End Try

        End If

    Catch ex As Exception

        result = Dts.TaskResult = ScriptResults.Failure

        Call Dts.Events.FireError( _
            0, _
            String.Empty, _
            ex.Message, _
            String.Empty, _
            0)

    Finally

        Call ftpClient.Close()

    End Try

    Dts.TaskResult = ScriptResults.Success

End Sub ' Main

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-12-24 08:29:39

我认为您不能直接将 fileList 值分配给对象变量。您需要循环遍历这些值并将其分配给集合变量,然后再将其分配给 SSIS 包的对象变量。

以下链接中的答案包含如何循环遍历 FTP 文件然后将它们存储在对象变量中的详细说明。

当没有文件可供下载时,如何避免 SSIS FTP 任务失败?

下面显示的代码取自上面的链接。该代码查找特定的文件模式,而不是下载所有文件。这是与您想要做的事情的唯一偏差。

可在 SSIS 2005 及更高版本 中使用的 VB 代码。

包含 Imports 语句 Imports System.Text.RegularExpressions

Public Sub Main()
    Dim varCollection As Variables = Nothing
    Dim ftpManager As ConnectionManager = Nothing
    Dim ftpConnection As FtpClientConnection = Nothing
    Dim fileNames() As String = Nothing
    Dim folderNames() As String = Nothing
    Dim listOfFiles As Collections.ArrayList
    Dim remotePath As String = String.Empty
    Dim filePattern As String = String.Empty
    Dim regexp As Regex
    Dim counter As Integer

    Dts.VariableDispenser.LockForRead("User::RemotePath")
    Dts.VariableDispenser.LockForRead("User::FilePattern")
    Dts.VariableDispenser.LockForWrite("User::ListOfFiles")
    Dts.VariableDispenser.GetVariables(varCollection)

    Try

        remotePath = varCollection("User::RemotePath").Value.ToString()
        filePattern = varCollection("User::FilePattern").Value.ToString()

        ftpManager = Dts.Connections("FTP")
        ftpConnection = New FtpClientConnection(ftpManager.AcquireConnection(Nothing))

        ftpConnection.Connect()
        ftpConnection.SetWorkingDirectory(remotePath)
        ftpConnection.GetListing(folderNames, fileNames)            

        listOfFiles = New Collections.ArrayList()
        If fileNames IsNot Nothing Then
            regexp = New Regex("^" & filePattern & "$")
            For counter = 0 To fileNames.GetUpperBound(0)
                If regexp.IsMatch(fileNames(counter)) Then
                    listOfFiles.Add(remotePath & fileNames(counter))
                End If
            Next counter
        End If

        varCollection("User::ListOfFiles").Value = listOfFiles

        Dts.TaskResult = ScriptResults.Success

    Catch ex As Exception
        Dts.Events.FireError(-1, String.Empty, ex.ToString(), String.Empty, 0)
        Dts.TaskResult = ScriptResults.Failure
    Finally
        varCollection.Unlock()
        ftpConnection.Close()
        ftpConnection = Nothing
        ftpManager = Nothing
    End Try

    Dts.TaskResult = ScriptResults.Success
End Sub

希望有帮助。

I think that you cannot directly assign the fileList value to an object variable. You need to loop through the values and assign it to a collection variable before assigning it to an SSIS package's object variable.

Answer in the following link contains a detailed explanation of how to loop through FTP files and then store them in an object variable.

How to avoid SSIS FTP task from failing when there are no files to download?

Code shown below was taken from the above link. The code looks for specific file pattern instead of downloading all the files. That is the only deviation from what you are trying to do.

VB code that can be used in SSIS 2005 and above.

Include the Imports statement Imports System.Text.RegularExpressions

Public Sub Main()
    Dim varCollection As Variables = Nothing
    Dim ftpManager As ConnectionManager = Nothing
    Dim ftpConnection As FtpClientConnection = Nothing
    Dim fileNames() As String = Nothing
    Dim folderNames() As String = Nothing
    Dim listOfFiles As Collections.ArrayList
    Dim remotePath As String = String.Empty
    Dim filePattern As String = String.Empty
    Dim regexp As Regex
    Dim counter As Integer

    Dts.VariableDispenser.LockForRead("User::RemotePath")
    Dts.VariableDispenser.LockForRead("User::FilePattern")
    Dts.VariableDispenser.LockForWrite("User::ListOfFiles")
    Dts.VariableDispenser.GetVariables(varCollection)

    Try

        remotePath = varCollection("User::RemotePath").Value.ToString()
        filePattern = varCollection("User::FilePattern").Value.ToString()

        ftpManager = Dts.Connections("FTP")
        ftpConnection = New FtpClientConnection(ftpManager.AcquireConnection(Nothing))

        ftpConnection.Connect()
        ftpConnection.SetWorkingDirectory(remotePath)
        ftpConnection.GetListing(folderNames, fileNames)            

        listOfFiles = New Collections.ArrayList()
        If fileNames IsNot Nothing Then
            regexp = New Regex("^" & filePattern & "$")
            For counter = 0 To fileNames.GetUpperBound(0)
                If regexp.IsMatch(fileNames(counter)) Then
                    listOfFiles.Add(remotePath & fileNames(counter))
                End If
            Next counter
        End If

        varCollection("User::ListOfFiles").Value = listOfFiles

        Dts.TaskResult = ScriptResults.Success

    Catch ex As Exception
        Dts.Events.FireError(-1, String.Empty, ex.ToString(), String.Empty, 0)
        Dts.TaskResult = ScriptResults.Failure
    Finally
        varCollection.Unlock()
        ftpConnection.Close()
        ftpConnection = Nothing
        ftpManager = Nothing
    End Try

    Dts.TaskResult = ScriptResults.Success
End Sub

Hope that helps.

愛上了 2024-12-24 08:29:39

事实证明这是64位模式造成的问题。我更改 Run64BitRuntime = False 后,一切正常!

It turns out that this was a problem created by 64bit mode. After I changed Run64BitRuntime = False, everything worked properly!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文