要求是执行SSIS软件包,当文件到达文件夹时,我不想手动启动该软件包。
不确定文件到达的时间,文件是否可以多次到达。每当文件到达时,都必须将其加载到表中。我认为,某些解决方案(如文件监视程序任务)仍然希望启动程序包
过去我这样做的方法是使用一个从SQL Server Agent调用的无限循环包。
这是我的无限循环软件包:
设置3个变量:
IsFileExists-布尔值-0
FolderLocation-字符串-C:\要放置文件的位置\
IsFileExists布尔值-0
对于For循环容器:
IsFileExists
如上设置变量。
使用ReadOnlyVariable设置C#脚本任务,User::FolderLocation
并具有以下内容:
public void Main()
{
int fileCount = 0;
string[] FilesToProcess;
while (fileCount == 0)
{
try
{
System.Threading.Thread.Sleep(10000);
FilesToProcess = System.IO.Directory.GetFiles(Dts.Variables["FolderLocation"].Value.ToString(), "*.txt");
fileCount = FilesToProcess.Length;
if (fileCount != 0)
{
for (int i = 0; i < fileCount; i++)
{
try
{
System.IO.FileStream fs = new System.IO.FileStream(FilesToProcess[i], System.IO.FileMode.Open);
fs.Close();
}
catch (System.IO.IOException ex)
{
fileCount = 0;
continue;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
// TODO: Add your code here
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
这实际上是监视.txt文件的文件夹位置,如果该文件不存在,它将休眠10秒钟(如果需要,可以增加此长度)。如果文件确实存在,它将完成,然后程序包将执行装入程序包。但是,它将继续运行,因此,下次将文件放入其中时,它将再次执行装入程序包。
确保将这个永久循环程序包作为sql服务器代理作业运行,这样它将一直运行,我们正在运行类似的程序包,并且绝不会引起任何问题。
另外,请确保您的输入包将文件从放置文件夹位置移开/存档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句