Search Suggest

Call SSIS 2012 Package within .Net application - Part 1

Case
I want to start a SSIS 2012 package from a .Net application. How do you do that?

Solution
For this example I will use Visual Studio 2010 (same as for SSIS 2012) and a C# Windows Form Application for this example. I will add the VB.Net code as well.

Note: this example cannot handle (project) parameters that are available in 2012. See this example for project deployed packages.

1) Create project
Start Visual Studio 2010 and create a new C# Windows Form Application project.
C# Windows Form Application


















2) Change framework version
Changed the framework version to 4 in the newley created project. Right click the project and select properties. A new window will open and there you can change the Target framework to 4. Confirm the question about changing the framework version
Changing framework version


















3) Adding reference to SSIS
We need a reference to Microsoft.SqlServer.ManagedDts.dll to call a package. Right click References in the Solution Explorer and click "Add Reference".  Now browse to the 2012 version of ManagedDts.dll. See screendump for location.
Add reference to SSIS


















4) Edit app.Config
Because apparently this new referenced dll is some kind of mixed mode compiled dll, you need to add useLegacyV2RuntimeActivationPolicy="true" to the app.Config. If you don't do this you will get the following error in SSIS:
The Execute method on the task returned error code 0x80131621 (Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.). The Execute method must succeed, and indicate the result using an "out" parameter.
app.Config


















5) Hello World form
I created a very simple form with a start button (btnStart) and a label (lblStatus) to show the execution result.
My Windows application


















6) Code for start button
This is the code for the start button.
//C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Runtime; // Added

namespace CallSSIS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnStart_Click(object sender, EventArgs e)
{
// Instantiate SSIS application object
Microsoft.SqlServer.Dts.Runtime.Application myApplication = new Microsoft.SqlServer.Dts.Runtime.Application();

// Load package from file system (use LoadFromSqlServer for SQL Server based packages)
lblStatus.Text = "Loading package from file system.";
Package myPackage = myApplication.LoadPackage(@"D:\SSIS\MyPackage.dtsx", null);

// Optional set the value from one of the SSIS package variables
myPackage.Variables["User::myVar"].Value = "test123";


// Execute package
lblStatus.Text = "Executing package";
DTSExecResult myResult = myPackage.Execute();

// Show the execution result
lblStatus.Text = "Package result: " + myResult.ToString();
}
}
}


'VB.Net code
Imports Microsoft.SqlServer.Dts.Runtime

Public Class Form1

Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
' Instantiate SSIS application object
Dim myApplication As New Microsoft.SqlServer.Dts.Runtime.Application()

' Load package from file system (use LoadFromSqlServer for SQL Server based packages)
lblStatus.Text = "Loading package from file system."
Dim myPackage As Package = myApplication.LoadPackage("D:\SSIS\MyPackage.dtsx", Nothing)

' Optional set the value from one of the SSIS package variables
myPackage.Variables("User::myVar").Value = "test123"

' Execute package
lblStatus.Text = "Executing package"
Dim myResult As DTSExecResult = myPackage.Execute()

' Show the execution result
lblStatus.Text = "Package result: " & myResult.ToString()
End Sub
End Class

The result
Build and run the application.

Post a Comment