Executing SSIS task from C# application

2019-06-14 13:27发布

问题:

I can successfully run the SSIS package from my C# App. Is there a way to run a specific task within the SSIS package from .NET (C#) Application?

回答1:

We did something like this with an ASP.NET Web Forms application a few years back basically by creating a SQL Agent Job with just one step that executed the SSIS package that had been deployed to the server and then invoking it via the Enterprise Library

    public bool ExecutePackage(string jobName)
    {
        int result = -1;
        bool success = false;

        try
        {
            // "SsisConnectionString" will be the name of your DB connection string in your config
            Database db = DatabaseFactory.CreateDatabase("SsisConnectionString");  
            using (DbCommand dbCommand = db.GetStoredProcCommand("sp_start_job"))
            {
                db.DiscoverParameters(dbCommand);
                db.SetParameterValue(dbCommand, "job_name", jobName);
                db.SetParameterValue(dbCommand, "job_id", null);
                db.SetParameterValue(dbCommand, "server_name", null);
                db.SetParameterValue(dbCommand, "step_name", null);
                db.ExecuteNonQuery(dbCommand);
                result = Convert.ToInt32(db.GetParameterValue(dbCommand, "RETURN_VALUE"));
            }
        }
        catch (Exception exception)
        {
            success = false;
        }

        switch (result)
        {
            case 0:
                success = true;
                break;
            default:
                success = false;
                break;
        }

        return success;
    }

And in your config:

<connectionStrings>
    <add name="SsisConnectionString"
         connectionString="Data Source=<server>;Initial Catalog=MSDB;User Id=<user>;Password=<pwd>;"
         providerName="System.Data.SqlClient"/>
</connectionStrings>


回答2:

I think you can open your package using the API, disable all other tasks and then run the whole package



回答3:

I found a way to access Package's Tasks and set properties to it.

var task = (TaskHost)package.Executables["Your Package Name"];

task.Properties["Any Property"].SetValue(task, "Property Value");

Thanks for everyone's input anyway.