我在SSIS和Visual Studio 2008中工作.执行时,我需要让SSIS包执行以下任务:
该包将由其他一些脚本运行.因此,每次包运行时我们都需要它来检查文件.我们正在尝试阻止我们必须监视文件夹并在文件出现时手动执行包的情况.
有什么建议?
最简单的方法是设置一个Foreach循环容器,其中包含所有包的"工作"(可选地,您可以将其作为前驱步骤并使用它的条件表达式).假设您有两个名为FileName的变量(这是您将赋值的值)和一个包含"where"的InputFolder变量
ForEach Loop Editor
Collection tab:
Enumerator = Foreach File Enumerators
Expression: Directory = @[User:InputFolder]
FileSpec: "YD.*"
Retrieve file name
* Fully qualified
Variable Mappings tab:
Variable: User::FileName
Index: 0
Run Code Online (Sandbox Code Playgroud)

您也可以通过脚本任务执行此操作,如果您想查看,请告诉我.
编辑此脚本再次假定您已定义变量InputFolder和FileName.创建一个脚本任务组件并将InputFolder检查为只读变量,将FileName检查为读/写变量.
using System;
using System.Data;
using System.IO; // this needs to be added
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
// namespace will vary
namespace ST_bc177fa7cb7d4faca15531cb700b7f11.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
string inputFolder;
string fileName;
inputFolder = Dts.Variables["InputFolder"].Value.ToString();
// File, if exists will look like YD.CCYYMMDD.hhmmss.done
string fileMask = "YD.*.done";
// this array will catch all the files matching a given pattern
string[] foundFiles = null;
foundFiles = System.IO.Directory.GetFiles(inputFolder, fileMask);
// Since there should be only one file, we will grab the zeroeth
// element, should it exist
if (foundFiles.Length > 0)
{
fileName = foundFiles[0];
// write the value to our global SSIS variable
Dts.Variables["FileName"].Value = fileName;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这是一个可能的选择.您可以使用Foreach Loop容器实现此目的.请找到我在下面提供的示例.希望,这给了一个想法.
分步过程:
在SSIS包中,创建3个变量显示在屏幕截图#1中.范围CheckFile表示包名称.变量Folder将表示您要检查文件的文件夹.Filename表示要检查的文件名.变量FilePath将是您需要的全局变量.如果文件存在,它将填入文件路径值,否则它将为空.
在包的"控制流"选项卡上,放置a Foreach Loop container和a Script Task.Script Task用于展示变量在Foreach循环容器执行完成后保留该值.参见截图#2.
配置ForEach循环容器,如屏幕截图#3和#4所示.
用本节下给出的代码替换S中的Main()方法.这是为了演示变量保留的值.cript TaskScript task codeFilePath
屏幕截图#5显示路径中不存在文件c:\temp\,屏幕截图#6显示相应的包执行.
屏幕截图#7显示文件TestFile.txt存在于路径中c:\temp\,屏幕截图#8显示相应的包执行.
如果您希望在文件存在时处理该文件,您可以Data Flow Task在其中放置一个文件Foreach Loop container.
希望有所帮助.
脚本任务代码:
只能在SSIS 2008 and above..中使用的C#代码
public void Main()
{
Variables varCollection = null;
Dts.VariableDispenser.LockForRead("User::FilePath");
Dts.VariableDispenser.GetVariables(ref varCollection);
if (String.IsNullOrEmpty(varCollection["User::FilePath"].Value.ToString()))
{
MessageBox.Show("File doesn't exist.");
}
else
{
MessageBox.Show("File " + varCollection["User::FilePath"].Value.ToString() + " exists.");
}
Dts.TaskResult = (int)ScriptResults.Success;
}
Run Code Online (Sandbox Code Playgroud)
截图#1:

截图#2:

截图#3:

截图#4:

截图#5:

截图#6:

截图#7:

截图#8:
