如何通过C#以编程方式运行多个SSIS包?

时间:2014-06-23 12:59:52

标签: c# .net sql-server ssis

家伙,我使用了这个代码片段,我执行了多个SSIS包,当第一个完成时,执行另一个需要很长时间,但是我在CMD上运行脚本命令行,它会快速执行。所以我认为这是代码问题,你知道为什么吗?这是我的代码:

SSISHelper.ExecuteSSISPackage("/F \"C:\\Users\\v-nashi\\Documents\\visual studio 2010\\projects\\ImportExcel\\ImportExcel\\LYO_DailyLogin.dtsx\"");
SSISHelper.ExecuteSSISPackage("/F \"C:\\Users\\v-nashi\\Documents\\visual studio 2010\\projects\\ImportExcel\\ImportExcel\\LYO_COSMOS_Activities.dtsx\"");


    /// <summary>
    /// Excuete SQL Server Integration Services packages with parameter.
    /// </summary>
    /// <param name="para">parameter</param>
    /// <returns>bool</returns>
    public static bool ExecutePackage(string parameter)
    {
        if (File.Exists(DTExec_Path) == false)
            throw new Exception("The file DTExec.exe is not found, or the file is not exist.");

        Process process = new Process();
        process.StartInfo.FileName = DTExec_Path;
        process.StartInfo.Arguments = parameter;
        // True if the shell should be used when starting the process; false if the process should be created directly
        // from the executable file.
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;

        Console.WriteLine("{0} {1}", DTExec_Path, parameter);

        process.Start();
        process.WaitForExit();

        string[] results = process.StandardOutput.ReadToEnd().Split('\n');
        foreach (string item in results)
        {
            if (item.Contains("DTExec: The package execution returned DTSER_SUCCESS (0)."))
                return true;
        }

        return false;
    }

我只是想以编程方式运行SSIS包,或者更好的方式?

1 个答案:

答案 0 :(得分:0)

另一种方式可能是这样的。

Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();

            string packagePath = "Path of your SSIS package";

            Package package = app.LoadPackage(packagePath, null);

            //Assign your variables here.
            Variables vars = package.Variables;
            vars["FileName"].Value = variables.FileName;



            Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = package.Execute();

            if (results == DTSExecResult.Success)
            {
               //Do what u want after success.
            }

为此,必须使用来自microsoft的这个Microsoft.SqlServer.ManagedDTS库。尝试在GAC或其他网站上找到它。 这是针对单个SSIS服务的方式,U可以逐个执行多个。