2012-02-22 84 views
0

我創建了一個包來從FTP服務器導入文件,並且只需要處理新文件,即排除已經加載的文件(保存在表中)。SSIS腳本任務 - 未能填寫DataTable

我首先運行一個執行SQL任務來填充「AlreadyLoadedFiles」對象變量。然後,我嘗試確定在腳本任務中需要處理哪些文件。我首先加載FTP服務器上的文件名,然後刪除那些已經加載的文件。

我沒有問題檢索FTP上的文件的名稱,但問題是,當我用對象變量「AlreadyLoadedFiles」填充OleDBDataAdapter時,生成的數據表爲空,我不知道爲什麼。

' Microsoft SQL Server Integration Services Script Task 
' Write scripts using Microsoft Visual Basic 
' The ScriptMain class is the entry point of the Script Task. 

Imports System 
Imports System.Data 
Imports System.Math 
Imports Microsoft.SqlServer.Dts.Runtime 
Imports System.Xml 
Imports System.Data.OleDb 
Imports System.Collections.Specialized 
Imports System.Text.RegularExpressions 

<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _ 
<System.CLSCompliantAttribute(False)> _ 
Partial 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() 
     ' 

     Dim vs As Variables 
     Dim dt As New DataTable 
     Dim da As New OleDbDataAdapter() 


     'We need to go to or FTP server 
     Dts.VariableDispenser.LockOneForRead("FTPSourceDirectory", vs) 

     Dim cm As ConnectionManager = Dts.Connections("FTPServer") 
     Dim ftp As FtpClientConnection = New FtpClientConnection(cm.AcquireConnection(Nothing)) 
     ftp.Connect() 
     ftp.SetWorkingDirectory(vs("FTPSourceDirectory").Value.ToString()) 
     vs.Unlock() 

     'We now need to get ourselves the files we have already seen 
     Dts.VariableDispenser.LockOneForRead("AlreadyLoadedFiles", vs) 


     da.Fill(dt, vs("AlreadyLoadedFiles").Value) 
     MessageBox.Show(dt.Rows.Count) 
     vs.Unlock() 


     Dim foldernames() As String 
     Dim filenames() As String 

     'Get the list of files that are there on the FTP server 
     ftp.GetListing(foldernames, filenames) 

     Dim dr As DataRow 

     Dim ss As StringCollection = New StringCollection() 

     Dim iFileCount As Integer 

     If filenames Is Nothing Then 
      MessageBox.Show("No Files Found") 
      Exit Sub 
     Else 
      'Need to loop through all the files found 
      For iFileCount = 0 To filenames.GetUpperBound(0) 

       'First we add all of the found files to the Array (Object) but only if they are CSV files 

       Dim re As New Regex("^co_users_report_\d{4}-\d{2}-\d{2}\.csv$") 

       If re.IsMatch(filenames(iFileCount).ToString()) Then 
        ss.Add(filenames(iFileCount).ToString()) 
        Dts.Events.FireInformation(0, "", filenames(iFileCount).ToString(), "", 0, True) 
       End If 
       For Each dr In dt.Rows 
        Dts.Events.FireInformation(0, "", dr(0).ToString(), "", 0, True) 
        If dr(0).ToString() = filenames(iFileCount).ToString() Then 
         MessageBox.Show(dr(0).ToString) 
         Dts.Events.FireInformation(0, "", "Removed " & filenames(iFileCount).ToString() & " from array because it was previously loaded.", "", 0, True) 

         ss.Remove(filenames(iFileCount).ToString()) 
         Exit For 
        End If 
       Next 
      Next 
     End If 

     Dts.VariableDispenser.LockOneForWrite("FilesForFTPDownload", vs) 

     vs(0).Value = ss 


     vs.Unlock() 

     Dts.TaskResult = ScriptResults.Success 
    End Sub 

End Class 

我使用SQL Server(SSIS)2008 R2 64位

回答

0

我建議你闖入多個組件此。有一個腳本任務,就像你上面的腳本任務一樣,從ftp站點提取必要的數據並將其保存爲一個平面文件。然後構建使用數據流任務來讀取平面文件並進行相應處理。儘管您所採用的方式是有效的,但我認爲它不像使用額外的SSIS組件那樣高效。

0

我確實發現了這個問題 - 這是審計框架重置ADO Rowset ...