2013-11-27 38 views
0

Good Day All,導入SSIS中的最後一個修改文件

對於許多人可能是一個非常簡單的問題,我提前表示歉意。

基本上每個小時都會將文件保存到某個文件夾(文件擴展名爲.AMA),並且我想創建一個每小時運行一次的SSIS包,並只將最後一個修改後的文件導入到SQL Server數據庫中。

我意識到我需要使用腳本組件要做到這一點,但我有vb.net的零工作經驗(我被困在VS 2005)。另外,我不確定這是否需要在Foreach循環容器內完成,或者是否可以直接從腳本組件到OLE DB目標?

會有人好心地給我一個示例腳本,我可以工作了,並且向我解釋如何將其納入的SSIS包?我無法制作我從Google上看到的腳本解決方案的頭像和故事,而且他們中的許多人似乎都在使用C#。

上次修改日期/時間應該沒關係,但文件名中有一個日期/時間格式爲「YYMMDDHHMM」,我不確定這將是多麼有用。

提前致謝!

+0

您是否考慮過使用[文件系統任務](http://technet.microsoft.com/en-us/library/ms140185.aspx)?您可以將您的*源連接*定義爲來自變量。 – TsSkTo

+0

我還沒有,但我是否仍然需要使用腳本組件來定義變量?感謝您的迴應。的 – DPPD

+0

可能重複[導入最新的CSV文件到SQL Server的SSIS(http://stackoverflow.com/questions/8831060/import-most-recent-csv-file-to-sql-server-in-ssis) – billinkc

回答

0

我們沒有給出完整的解決方案,而是讓這個項目分解成小塊。嘗試解決這些問題,解決方案應該出現 - 希望。如果您遇到任何問題,請回復一些更尖銳的問題。這就是說,這是我的建議 - 1.取出一個.AMA文件。使用數據流任務。使用平面文件源連接作爲源,OleDB作爲目標。在連接管理器中對源連接和目標連接進行硬編碼。如果您需要任何轉換,請嘗試使用派生列(因爲它更容易並且可以完成大部分轉換)。如果你能完成這件作品,它會消除你對使用腳本組件的懷疑。

  1. 接下來,開始去除步驟一中提到的硬編碼部分。瞭解如何使用變量動態更改連接字符串。

  2. 將另一個.AMA文件放在同一位置。使用ForEach任務來處理這兩個文件。 (不一定是你會需要它)

  3. Import most recent csv file to sql server in ssis

希望這有助於你自己在找到解決方案 - 而不是要求一個完整的解決方案。

+0

嗨Anoop Verma,這是你的迴應。我已經使用Foreach循環容器中的變量,然後使用平面文件源來導入另一個目錄中的所有文件,這沒有問題。我的問題是編寫腳本來識別目錄中最近修改的文件。你在第三步中鏈接到的線程是我在我的谷歌搜索中遇到的第一個東西,但它使用C#腳本來做到這一點,我不知道如何在Visual Studio 2005中的腳本組件中應用它。謝謝。 – DPPD

+0

感謝您回覆。該代碼有很好的記錄。我使用了一個代碼轉換器網站。這是輸出。 –

0
Public Sub Main() 
Dim fileMask As String = "*.csv" 
Dim mostRecentFile As String = String.Empty 
Dim rootFolder As String = String.Empty 

' Assign values from the DTS variables collection. 
' This is case sensitive. User:: is not required 
' but you must convert it from the Object type to a strong type 
rootFolder = Dts.Variables["User::RootFolder"].Value.ToString() 

' Repeat the above pattern to assign a value to fileMask if you wish 
' to make it a more flexible approach 

' Determine the most recent file, this could be null 
Dim candidate As System.IO.FileInfo = ScriptMain.GetLatestFile(rootFolder, fileMask) 

If candidate IsNot Nothing Then 
    mostRecentFile = candidate.FullName 
End If 

' Push the results back onto the variable 
Dts.Variables["CurrentFile"].Value = mostRecentFile 

Dts.TaskResult = (int)ScriptResults.Success 
End Sub 




private static System.IO.FileInfo GetLatestFile(string directoryName, string fileExtension) 
{ 
System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(directoryName); 

System.IO.FileInfo mostRecent = null; 

// Change the SearchOption to AllDirectories if you need to search subfolders 
System.IO.FileInfo[] legacyArray = directoryInfo.GetFiles(fileExtension,  
    System.IO.SearchOption.TopDirectoryOnly); 
foreach (System.IO.FileInfo current in legacyArray) 
{ 
    if (mostRecent == null) 
    { 
     mostRecent = current; 
    } 

    if (current.LastWriteTimeUtc >= mostRecent.LastWriteTimeUtc) 
    { 
     mostRecent = current; 
    } 
} 

return mostRecent; 

// To make the below code work, you'd need to edit the properties of the project 
// change the TargetFramework to probably 3.5 or 4. Not sure 
// Current error is the OrderByDescending doesn't exist for 2.0 framework 
//return directoryInfo.GetFiles(fileExtension) 
//  .OrderByDescending(q => q.LastWriteTimeUtc) 
//  .FirstOrDefault(); 
} 

This is the site I used for conversion: http://www.developerfusion.com/tools/convert/csharp-to-vb