如何从c#执行多个ssis包

时间:2013-06-28 06:34:31

标签: c# ssis

我创建了10个不同的包,我想从c#编码中执行它们。有人可以发布一些屏幕截图来实现这一目标。 我试过这个

Application app = new Application();
        TraceService("loading system From File system");
        //Create package Container to hold the package.
        //And Load the Package Using the Application Object.
        Package package = app.LoadPackage(@"C:\User\Kiran\Documents\Visual Studio 2012\Projects\WindowsServiceTest\WindowsServiceTest\Package1.dtsx", null);

        TraceService("Execution Started");
        DTSExecResult result = package.Execute();
        // print the result
        TraceService(result.ToString());
        TraceService("Execution Completed");

这里我必须在运行时获取文件名而不是硬编码

2 个答案:

答案 0 :(得分:0)

以下代码将执行给定文件夹中的所有包。

var pkgLocation = @"C:\User\Kiran\Documents\Visual Studio 2012\Projects\WindowsServiceTest\WindowsServiceTest\";
foreach (var file in Directory.EnumerateFiles(pkgLocation, "*.dtsx"))
using (var pkg = new Application().LoadPackage(file, null))
{
   var pkgResults = pkg.Execute();
  Console.WriteLine("Package File Name:{0}, Result:{1}",file.ToString(), pkgResults.ToString());
}

答案 1 :(得分:0)

official site中很好地记录了C#和VB中正在执行的SSIS程序包。这是我执行多个SSIS包的脚本任务中的完整代码。

string packagesFolder = Dts.Variables["User::packagesFolder"].Value.ToString();
string rootFolder = Dts.Variables["User::rootFolder"].Value.ToString();

Package pkg;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;

foreach (var pkgLocation in Directory.EnumerateFiles(packagesFolder+"\\", "ValidateDataMigration-*.dtsx"))
{
    try
    {
        app = new Microsoft.SqlServer.Dts.Runtime.Application();
        pkg = app.LoadPackage(pkgLocation, null);
        pkgResults = pkg.Execute();

        File.AppendAllText(rootFolder + "\\DataValidationProgress.log", pkgLocation.ToString()+"=>"+ pkgResults.ToString()+ Environment.NewLine);
    }
    catch(Exception e)
    {
        File.AppendAllLines(rootFolder + "\\DataValidationErrors.log", new string[] { e.Message, e.StackTrace });
    }
}