2013-11-27 103 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