.net 导入SSIS中上次修改的文件
大家好 对于你们中的许多人来说,这可能是一个相当简单的问题,我提前表示歉意 基本上,每个小时都会将一个文件保存到某个文件夹(文件ext.AMA)中,我想创建一个每小时运行一次的SSIS包,只将最后修改的文件导入SQL Server数据库 我意识到我需要使用脚本组件来实现这一点,但我对vb.net没有任何工作知识(我被VS2005困住了)。另外,我不确定这是否需要在Foreach循环容器中完成,或者是否可以直接从scrip组件转到OLE DB目标 有没有人能给我一个可以编写的示例脚本,并向我解释如何将其合并到SSIS包中?我无法理解我在谷歌上看到的脚本解决方案,而且其中许多似乎都在使用C# 按上次修改的日期/时间应该可以,但文件名中有一个日期/时间,格式为“YYMMDDHHMM”,我不确定这是否有用.net 导入SSIS中上次修改的文件,.net,sql,vb.net,ssis,.net,Sql,Vb.net,Ssis,大家好 对于你们中的许多人来说,这可能是一个相当简单的问题,我提前表示歉意 基本上,每个小时都会将一个文件保存到某个文件夹(文件ext.AMA)中,我想创建一个每小时运行一次的SSIS包,只将最后修改的文件导入SQL Server数据库 我意识到我需要使用脚本组件来实现这一点,但我对vb.net没有任何工作知识(我被VS2005困住了)。另外,我不确定这是否需要在Foreach循环容器中完成,或者是否可以直接从scrip组件转到OLE DB目标 有没有人能给我一个可以编写的示例脚本,并向我解释如
提前谢谢 与其给出完整的解决方案,不如让我们把这个项目分成几个小部分。尝试解决每一个问题,解决方案就会出现——希望如此。如果你在任何一件事情上遇到困难,你可以用更尖锐的问题来回答。也就是说,以下是我的建议- 1.拿一份你的.AMA文件。使用数据流任务。使用平面文件源连接作为源,使用OleDB作为目标。在连接管理器中硬编码源连接和目标连接。如果您需要任何转换,请尝试使用派生列(因为它更容易并完成大多数转换)。如果您能够完成这篇文章,它将消除您对使用脚本组件的疑虑
希望这能帮助您自己找到解决方案,而不是要求完整的解决方案。您是否考虑过使用解决方案?您可以将源连接定义为来自变量。我没有,但我是否仍然需要使用脚本组件来定义变量?感谢您的回复。查询的可能副本可能是相同的,但该线程中的答案使用的是C#,我不能使用它,因为我使用的是Visual Studio 2005,所以我需要vb.net中的脚本。我不是程序员,所以如果我遗漏了什么,我道歉。嗨,Anoop Verma,这是你的回答。我以前使用过Foreach循环容器中的变量,使用平面文件源导入另一个目录中的所有文件,这没有问题。我的问题是编写脚本来标识目录中最近修改的文件。您在第三步中链接到的线程是我在google quest中遇到的第一件事,但它使用C#脚本来实现这一点,我不知道如何在visual studio 2005的脚本组件中应用它?谢谢,谢谢你回来。这段代码有很好的文档记录。我使用了一个代码转换器站点。这是输出。
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