脚本任务错误 - 无法将 GetListings 文件列表放入对象变量中
使用下面的脚本任务将文件列表填充到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不能直接将 fileList 值分配给对象变量。您需要循环遍历这些值并将其分配给集合变量,然后再将其分配给 SSIS 包的对象变量。
以下链接中的答案包含如何循环遍历 FTP 文件然后将它们存储在对象变量中的详细说明。
当没有文件可供下载时,如何避免 SSIS FTP 任务失败?
下面显示的代码取自上面的链接。该代码查找特定的文件模式,而不是下载所有文件。这是与您想要做的事情的唯一偏差。
可在
SSIS 2005 及更高版本
中使用的 VB 代码。包含
Imports
语句 Imports System.Text.RegularExpressions希望有帮助。
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.RegularExpressionsHope that helps.
事实证明这是64位模式造成的问题。我更改 Run64BitRuntime = False 后,一切正常!
It turns out that this was a problem created by 64bit mode. After I changed Run64BitRuntime = False, everything worked properly!