【问题标题】:Importing last modified file in SSIS在 SSIS 中导入最后修改的文件
【发布时间】:2013-11-27 09:42:44
【问题描述】:

大家好,

对于对你们中的许多人来说可能是一个非常简单的问题,我提前道歉。

基本上每小时都会将一个文件保存到某个文件夹(文件扩展名为 .AMA)中,我想创建一个每小时运行一次的 SSIS 包,并且只将最后修改的文件导入 SQL Server 数据库。

我意识到我需要使用脚本组件来执行此操作,但我对 vb.net 的工作知识为零(我坚持使用 VS 2005)。另外,我不确定这是否需要在 Foreach 循环容器中完成,或者是否可以直接从脚本组件到 OLE DB 目标?

谁能给我一个我可以处理的示例脚本,并向我解释如何将它合并到 SSIS 包中?我对从谷歌搜索中看到的脚本解决方案一无所知,而且其中许多似乎无论如何都在使用 C#。

按上次修改的日期/时间应该没问题,但是文件名中有一个日期/时间,格式如下“YYMMDDHHMM”,但我不确定这会有多大用处。

提前致谢!

【问题讨论】:

  • 您是否考虑过使用File System Task?您可以将您的源连接定义为来自变量。
  • 我没有,但我不会仍然需要使用脚本组件来定义变量吗?感谢您的回复。
  • 查询可能是相同的,但该线程中的答案是使用 C#,我不能使用它,因为我使用的是 Visual Studio 2005,所以我需要 vb.net 中的脚本。我不是程序员,所以如果我遗漏了什么我道歉。

标签: .net sql vb.net ssis


【解决方案1】:

让我们将这个项目分解成更小的部分,而不是给出完整的解决方案。尝试解决这些问题中的每一个,并且应该会出现解决方案-希望如此。如果你卡在任何一块上,感觉回到更尖锐的问题。也就是说,这是我的建议 - 1. 获取您的 .AMA 文件之一。使用数据流任务。使用平面文件源连接作为源,使用 OleDB 作为目标。在连接管理器中硬编码源连接和目标连接。如果您需要任何转换,请尝试使用派生列(因为它更容易并且可以完成大部分转换)。如果你能完成这篇文章,它将消除你对使用脚本组件的疑虑。

  1. 接下来,移除第一步中提到的硬编码部分。了解如何使用变量动态更改连接字符串。

  2. 将另一个 .AMA 文件放在同一位置。使用 ForEach 任务来处理这两个文件。 (不一定你会需要它)

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

希望这可以帮助您自己找到解决方案 - 而不是要求完整的解决方案。

【讨论】:

  • 嗨 Anoop Verma,这是您的回复。我之前使用过 Foreach 循环容器中的变量来导入另一个目录中的所有文件,使用平面文件源,这没问题。我的问题是编写脚本来识别目录中最近修改的文件。您在第三步中链接到的线程是我在谷歌搜索中遇到的第一件事,但它使用 C# 脚本来执行此操作,我不知道如何在 Visual Studio 2005 的脚本组件中应用它?谢谢。
  • 感谢您回复。该代码有很好的文档记录。我使用了代码转换器站点。这是输出。
【解决方案2】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多