Executing SSIS task from C# application

2019-06-14 13:02发布

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?

3条回答
劫难
2楼-- · 2019-06-14 13:41

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.

查看更多
唯我独甜
3楼-- · 2019-06-14 13:44

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

查看更多
男人必须洒脱
4楼-- · 2019-06-14 13:47

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>
查看更多
登录 后发表回答