2010-10-08 26 views
0

在SSIS 2005中,我正在使用FTP任務。我有一個流程,當程序包運行時,它從FTP檢索特定文件夾中的任何文件到本地文件夾。在SSIS中的某些特定錯誤上繼續流動

遠程文件夾的路徑是通過變量設置如/root/abc/*abc.txt

如果有文件,該文件夾中匹配此標準的任務工作正常。如果沒有文件,則任務將失敗,並顯示文件未找到錯誤!

我該如何讓SSIS不會中斷任務,以防萬一這個特定的文件沒有找到錯誤出現,只是因爲遠程文件夾沒有匹配的文件?

但是,如果有錯誤,如FTP服務器無法登錄等,那麼任務應該拋出預期的錯誤。

回答

0

大概你現在已經找到了你的問題的答案。這是實現這一目標的一種可能方式。 Script Task可用於查找給定模式的FTP文件夾路徑中存在的文件列表(如*.txt)。下面的例子顯示瞭如何做到這一點。

步驟一步的過程:

  1. 在SSIS包,創建一個名爲FTP Connection FTP 中所示屏幕截圖#還創建變量。變量RemotePath包含FTP文件夾路徑; LocalPath包含將文件下載到的文件夾; FilePattern包含文件模式以查找從FTP服務器下載的文件列表; FileName將由Foreach loop container填充,但爲了避免FTP任務設計時出現錯誤,可以使用/DelayValidation屬性將FTP任務填充爲True

  2. 在SSIS包,地點如圖截圖#一個Script TaskForeach Loop containerFTP TaskForeach Loop container內。

  3. 與下腳本任務代碼部分中的代碼替換Script TaskMain()方法。腳本任務將使用與給定模式匹配的文件集合來填充變量ListOfFiles。這個例子首先使用模式* .txt,它不會產生任何結果,然後使用模式* .xls來匹配FTP服務器上的幾個文件。

  4. 配置在屏幕截圖中所示的Foreach Loop container作爲#3 #。該任務將通過變量** ListOfFiles *進行循環。如果沒有文件,則循環容器內的FTP任務將不會執行。如果有文件,循環容器內的FTP任務將執行FTP服務器上找到的文件數的任務。

  5. 配置在屏幕截圖中所示的FTP Task爲#和#6

  6. 截圖#顯示了樣品包執行時沒有匹配的文件被發現爲圖案*.txt

  7. 截圖#示出了前執行包的文件夾C:\temp\的內容。

  8. 屏幕截圖#顯示當找到匹配文件*.xls模式時的樣本包執行。

  9. 屏幕截圖#顯示FTP遠程路徑/Practice/Directory_New的內容。

  10. 截圖#顯示了文件夾的C:\temp\執行的包的內容。

  11. 截圖#示出了包裝失敗當與不正確遠程路徑提供。

  12. 屏幕截圖#顯示與包失敗相關的錯誤消息。

希望有所幫助。

腳本任務代碼:

C#代碼可以在SSIS 2008 and above使用。

使用System.Text.RegularExpressions包含using聲明;

public void Main() 
{ 
    Variables varCollection = null; 
    ConnectionManager ftpManager = null; 
    FtpClientConnection ftpConnection = null; 
    string[] fileNames = null; 
    string[] folderNames = null; 
    System.Collections.ArrayList listOfFiles = null; 
    string remotePath = string.Empty; 
    string filePattern = string.Empty; 
    Regex regexp; 
    int counter; 

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

    try 
    { 
     remotePath = varCollection["User::RemotePath"].Value.ToString(); 
     filePattern = varCollection["User::FilePattern"].Value.ToString(); 

     ftpManager = Dts.Connections["FTP"]; 
     ftpConnection = new FtpClientConnection(ftpManager.AcquireConnection(null)); 
     ftpConnection.Connect(); 
     ftpConnection.SetWorkingDirectory(remotePath); 
     ftpConnection.GetListing(out folderNames, out fileNames); 
     ftpConnection.Close(); 

     listOfFiles = new System.Collections.ArrayList(); 
     if (fileNames != null) 
     { 
      regexp = new Regex("^" + filePattern + "$"); 
      for (counter = 0; counter <= fileNames.GetUpperBound(0); counter++) 
      { 
       if (regexp.IsMatch(fileNames[counter])) 
       { 
        listOfFiles.Add(remotePath + fileNames[counter]); 
       } 
      } 
     } 

     varCollection["User::ListOfFiles"].Value = listOfFiles; 
    } 
    catch (Exception ex) 
    { 
     Dts.Events.FireError(-1, string.Empty, ex.ToString(), string.Empty, 0); 
     Dts.TaskResult = (int) ScriptResults.Failure; 
    } 
    finally 
    { 
     varCollection.Unlock(); 
     ftpConnection = null; 
     ftpManager = null; 
    } 

    Dts.TaskResult = (int)ScriptResults.Success; 
} 

VB代碼,可以在SSIS 2005 and above使用。

包括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) 
     ftpConnection.Close() 

     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 = Nothing 
     ftpManager = Nothing 
    End Try 

    Dts.TaskResult = ScriptResults.Success 
End Sub 

截圖#1:

1

截圖#2:

2

鈧reenshot#3:

3

截圖#4:

4

截圖#5:

5

截圖#6:

6

截圖#7:

7

截圖#8:

8

截圖#9:

9

截圖#10:

10

截圖#11:

11

截圖#12:

12

截圖#13:

13