在SSIS包中使用脚本任务进行FTP传输

3

大家好,

我有一个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

结束类


最好有一个“暂存”文件夹来存放文件。然后,您可以使用“for each file”来获取该文件夹中的任何文件,并将其传递给FTP任务,然后将其移出暂存文件夹,以便不会再次发送。 - Nick.McDermaid
1个回答

1

我刚写完问题后,就发现了几个问题。

  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. 我刚刚发现这个链接很有效:故障排除脚本任务
  3. 解决1和2,就解决了3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接