2012-08-13 123 views
2

所有,脚本任务中SSIS包到FTP

我有一个SSIS包(SQL Server 2008中),但这几个数据流和文件操作,最后将文件上传到FTP服务器。

我能够完成大部分工作,但最后一个关键部分。使用'数据流'时,将使用数据库中的数据生成文本文件。现在该文件在结尾处以时间戳压缩:“filename_08132012.zip”。

时间戳每天都在变化,所以它每次都有一个新文件上传到FTP服务器。所以,我不用“FTP任务”(我找不到一种方法来完成这项工作),而是有一个“脚本任务”,它查找当前日期的文件并将其上传到FTP服务器。以下是代码,但是我有两个与此相关的问题:

  1. 我想这个“脚本任务”拿起当天的文件例如:filename_08132012.zip,filename_08142012.zip。我有代码将日期转换为适当格式的字符串。但由于某种原因,事实并非如此。
  2. 如何逐步执行此脚本文件,Visual Studio是否允许使用VB.net代码?这个脚本任务GUI不允许一步步完成。
  3. 它将一个空文件上传到FTP服务器。一个0字节的文件。我如何解决这个问题?
' Microsoft SQL Server Integration Services Script Task 
' Write scripts using Microsoft Visual Basic 2008. 
' The ScriptMain is the entry point class of the script. 

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

<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _ 
<System.CLSCompliantAttribute(False)> _ 
Public Class ScriptMain 
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase 
Enum ScriptResults 
    Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success 
    Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
End Enum 


Public Sub Main() 
    ' 
    ' Add your code here 
    ' 

    Try 

     'Create the connection to the ftp server 

     Dim cm As ConnectionManager = Dts.Connections.Add("FTP") 

     'Set the properties like username & password 

     cm.Properties("ServerName").SetValue(cm, "172.24.97.21") 

     cm.Properties("ServerUserName").SetValue(cm, "bbxuser") 

     cm.Properties("ServerPassword").SetValue(cm, "blockbuster") 

     cm.Properties("ServerPort").SetValue(cm, "21") 

     cm.Properties("Timeout").SetValue(cm, "0") 'The 0 setting will make it not timeout 

     cm.Properties("ChunkSize").SetValue(cm, "1000") '1000 kb 

     cm.Properties("Retries").SetValue(cm, "1") 

     'create the FTP object that sends the files and pass it the connection created above. 

     Dim ftp As FtpClientConnection = New FtpClientConnection(cm.AcquireConnection(Nothing)) 

     'Connects to the ftp server 

     ftp.Connect() 

     'Build a array of all the file names that is going to be FTP'ed (in this case only one file) 

     Dim files(0) As String 


     Dim FileDate As Date 
     FileDate = System.DateTime.Today 

     files(0) = "D:\NCR\Apps\RBX.SSIS.BBX_Customer_Extract\Email_Campaign\Redbox_EmailCampaign_and_Analytics_Extract_" + FileDate.ToString("mmddyyyy") + ".zip" 

     'ftp the file 

     'Note: I had a hard time finding the remote path directory. I found it by mistake by creating both the FTP connection and task in the SSIS package and it defaulted the remote path setting in the FTP task. 

     ftp.SendFiles(files, "/Email Campaign", True, False) ' the True makes it overwrite existing file and False is saying that it is not transferring ASCII 

     ftp.Close() 

    Catch ex As Exception 

     Dts.TaskResult = ScriptResults.Failure 

    End Try 

    Dts.TaskResult = ScriptResults.Success 
End Sub 

末级

回答

0

右后我写了这个问题了,我想几件事情出来。

  1. 我使用了不正确的格式。正确的格式为FileDate.ToString( “MMDDYYYY”)+ “.ZIP”
files(0) = "D:\NCR\Apps\RBX.SSIS.BBX_Customer_Extract\Email_Campaign\Redbox_EmailCampaign_and_Analytics_Extract_" + FileDate.ToString("mmddyyyy") + ".zip" 
files(0) = "D:\NCR\Apps\RBX.SSIS.BBX_Customer_Extract\Email_Campaign\Redbox_EmailCampaign_and_Analytics_Extract_" + FileDate.ToString("MMddyyyy") + ".zip" 
  1. (见上文)
  2. 我刚刚发现这一点,它的工作原理:Troubleshoot Script Task
  3. 解决1 & 2 ,解决3