5

Afternoon,

Is it possible to execute a SSIS package using MVC? What I am creating is a web application and it would have a button, once clicked the SSIS package runs.

The SSIS project is already set up and deployed on the MSSQL server.

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
theJ
  • 395
  • 5
  • 25
  • Deployed to SSISDB or msdb? Does the web page need to wait for execution completion? Does the page need to pass parameters to influence package behaviour or is starting it sufficient? – billinkc Feb 13 '19 at 14:41
  • @theJ Upvote - good question. I'm curious, from a mis-use case, what is the expected behavior if the user clicks on the button multiple times or tries to run the job before it completes? Would it be possible to have the job run on a schedule every n minutes? – J Weezy Feb 13 '19 at 18:54

4 Answers4

5

SQL Server 2012+ offers a fantastic mechanism for managing packages and their execution via the Integration Services catalog, SSISDB.

The following code provides an example of running the package (Package2.dtsx) from the MyProjectName SSIS project living under the Demo folder with an IntensityLevel package parameter turned up to 11.

DECLARE @execution_id bigint;

EXEC SSISDB.catalog.create_execution
    @package_name = N'Package2.dtsx'
,   @execution_id = @execution_id OUTPUT
,   @folder_name = N'Demo'
,   @project_name = N'MyProjectName'
,   @use32bitruntime = False
,   @reference_id = NULL;

DECLARE @var0 int = 11;

EXEC SSISDB.catalog.set_execution_parameter_value
    @execution_id
,   @object_type = 30
,   @parameter_name = N'IntensityLevel'
,   @parameter_value = @var0;

DECLARE @var1 smallint = 1;

EXEC SSISDB.catalog.set_execution_parameter_value
    @execution_id
,   @object_type = 50
,   @parameter_name = N'LOGGING_LEVEL'
,   @parameter_value = @var1;

EXEC SSISDB.catalog.start_execution
    @execution_id;

The easy way to get a sample of how the above SQL should be built out is to open SQL Server Management Studio (SSMS) and configure a run of the SSIS package. Navigate to the Integration Services Catalog and find the package you want to run. Right click and select Execute...

Execute Package

The Configuration menu opens up and find the parameter(s) you want to specify. Provide a sample value but DO NOT CLICK OK. Instead, click that Script button and specify script to new window (or clipboard)

Configure Execution

Now you have the exact commands that SSMS would have issued to run your package. Take that code, use your parameterization method of choice for having your MVC program stubbing in the correct runtime value for your parameter and then wrap all of that TSQL up in a simple database call (ole, ado, odbc it won't matter)

billinkc
  • 59,250
  • 9
  • 102
  • 159
2

You could just create a stored procedure which you can call from your MVC app with SQLClient or so.. In this stored procedure you can then launch the SSIS package.(see below link for more detailed description on how to do this)

https://www.mssqltips.com/sqlservertip/2992/how-to-execute-an-integration-services-ssis-package-from-a-sql-server-stored-procedure/

Tim
  • 21
  • 2
  • That is just a console application. Not really what I am looking to achieve. – theJ Feb 13 '19 at 15:06
  • Why would that not apply to MVC, behind it is just .NET? it's just a call with System.Data.SqlClient to a stored procedure. You could also use EF to call the stored proc. – Tim Feb 13 '19 at 15:28
1

I created a stored procedure which runs the SSIS package. Within MVC it calls the stored procedure -

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index()
{
    //code that updates DB

    #region Run Stored Procedure
    //connect to the SQL server
    var connection = new SqlConnection(_configuration.GetConnectionString("DatabaseConnection"));
    //command that runs procedure on the SQL server
    var command = new SqlCommand("RebuildSelection", connection)
    {
        CommandType = CommandType.StoredProcedure,
        CommandText = "RebuildSelection"
    };
    //get text from stored procedure to show success/error messages
    SqlParameter text = new SqlParameter("@Text", SqlDbType.NVarChar, 1000)
    {
        //output as its displayed to the user
        Direction = ParameterDirection.Output
    };
    //add the params
    command.Parameters.Add(text);
    connection.Open();
    //run query
    command.ExecuteNonQuery();
    //used to return success/error messages to user
    ViewData["Message"] = text.Value;
    connection.Close();
    #endregion

    return View();
}

This link was a big help for creating the procedure.

This one helped in returning message to the user.

theJ
  • 395
  • 5
  • 25
0

C# can be used to execute a package deployed to SSISDB as follows. The initial connection created in the SqlConnection object will be the server where the package is deployed to. In the example below, the ObjectType of 30 is used for a package parameter. This can be changed to 20 for a parameter at the project level.

using System.Data.SqlClient;
using Microsoft.SqlServer.Management.IntegrationServices;
using System.Collections.ObjectModel;

        string folder = "Folder of Package";
        string project = "Project Of Package";
        string packageName = "PackageName.dtsx";
        string packageParameter = "ParameterValue";

        // server where package is deployed
        SqlConnection connString = new SqlConnection(@"Data Source=ServerWherePackageDeployed;Initial Catalog=SSISDB;Integrated Security=SSPI;");
        IntegrationServices ssisConnString = new IntegrationServices(connString);

        //create PackageInfo object for package to execute
        Microsoft.SqlServer.Management.IntegrationServices.PackageInfo package = ssisConnString.Catalogs["SSISDB"].Folders[folder].Projects[project].Packages[packageName];

        Collection <Microsoft.SqlServer.Management.IntegrationServices.PackageInfo.ExecutionValueParameterSet> parameterList = new Collection<Microsoft.SqlServer.Management.IntegrationServices.PackageInfo.ExecutionValueParameterSet>();

        //set logging level to basic
        parameterList.Add(new Microsoft.SqlServer.Management.IntegrationServices.PackageInfo.ExecutionValueParameterSet { ObjectType = 50, ParameterName = "LOGGING_LEVEL", ParameterValue = 1 });

        parameterList.Add(new Microsoft.SqlServer.Management.IntegrationServices.PackageInfo.ExecutionValueParameterSet { ObjectType = 30, ParameterName = "ParameterName", ParameterValue = packageParameter });

        package.Execute(false, null, parameterList);
userfl89
  • 4,610
  • 1
  • 9
  • 17
  • That is for a console app you cannot add `using Microsoft.SqlServer.Management.IntegrationServices;` into MVC – theJ Jun 13 '19 at 07:53