Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Executando a tarefa SSIS do aplicativo C#


Fizemos algo assim com um aplicativo ASP.NET Web Forms há alguns anos, basicamente, criando um SQL Agent Job com apenas uma etapa que executava o pacote SSIS que havia sido implantado no servidor e, em seguida, invocando-o por meio da 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;
    }

E na sua configuração:
<connectionStrings>
    <add name="SsisConnectionString"
         connectionString="Data Source=<server>;Initial Catalog=MSDB;User Id=<user>;Password=<pwd>;"
         providerName="System.Data.SqlClient"/>
</connectionStrings>