我在SSIS 2012包中创建了一个脚本任务,需要从FTP服务器获取文件并将它们存储在本地服务器的硬盘上。我在脚本任务中编写的代码如下:
Public Sub Main()
'
' Add your code here
'
Const localFile As String = "C:\InboundOrders\*.*"
Const remoteFile As String = "/remote/folder/filename.pgp"
Const host As String = "ftp://ftp1.commerceub.com"
Const user As String = "ourusername"
Const pw As String = "ourpassword"
Dim URI As String = host & remoteFile
Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI),
FtpWebRequest)
ftp.Credentials = New System.Net.NetworkCredential(user, pw)
ftp.KeepAlive = False
ftp.UseBinary = True
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse,
System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using
Dts.TaskResult = ScriptResults.Success
End Sub
当我运行包时,收到错误声明:
DTS Script Task has encountered an exception in user code.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr,
Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder
binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers,
CultureInfo culture, String[] namedParams)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder,
Object target, Object[] args, CultureInfo culture)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
我该怎么做才能解决上述错误?感谢您的帮助和指导。
更新:我在我的代码中指定了一个实际位于FTP服务器上的文件。我收到同样的错误。我应该采取不同的方法吗?