• subscribe
July 23, 2009 12:00 AM

Call a Windows Form from an SSIS Package

Create a UI for changing SSIS package variables and input parameters
SQL Server Pro
InstantDoc ID #102525
Downloads
102525.zip

The [Before] and [After] Script Task components have the same functionality: Each reads the package variables and shows their values in a Message Box. Listing 2 shows the code for the [Before] Script Task component.

Listing 2: Code for [Before] Script Task component

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    Public Sub Main()
        '
        System.Windows.Forms.MessageBox.Show("Package variables values before
           Windows Form call: " & vbCrLf _
                 & "Server_Name: " & Dts.Variables("Server_Name").Value.ToString & vbCrLf _
                 & "User Action: " & Dts.Variables("Action").Value.ToString & vbCrLf,
                    "Package Variables: BEFORE")
        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class

Now we need to add two classes with Windows Form code to the ScriptTask project in the [Call Windows Form] Script Task component. To do so, open the Script Task component for editing and select the Script option in the left pane. Click Design Script, click the ScriptTask project, and press the right mouse button to display the context menu. Explore the Add item, select the Add Class option, enter the class name in the opened dialog box, and click the Add button.

In my case I created two classes, Form1 and Class1. The Form1 class contains form-related code from Listing 1. Class1 contains partial class-definition code from the VB Windows Forms solution that was created at the beginning of this article. Next locate the Form1.vb and Form1.Designer.vb files in that project. Open those files and copy and paste the code from Form1.vb to the Form1 class and from Form1.Designer.vb to the Class1 class.

Next we'll add code for the Windows Form call to the ScriptMain class in the ScriptTask project; Listing 3 shows this code. Save the changes and close the project.

Listing 3: Code for [Windows Form Call] Script Task component

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Windows.Forms
Imports System.EventArgs

Public Class ScriptMain

    Public Sub Main()

        Dim frm As New Form1()

        frm._ServerName = Dts.Variables("Server_Name").Value.ToString()

        frm.ShowDialog()

        Dts.Variables("Server_Name").Value = frm._ServerName
        Dts.Variables("Action").Value = frm._Action

        frm.Dispose()

        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class


Then configure the [Call Windows Form] Script Task component. Since we will change package variables, we have to pass it to the Script Task component in a read-write mode. To do so, click the Script item in the left pane and set the ReadWriteVariables property to Server_Name,Action. Finally, save everything and execute the package.

A Versatile Solution
The solution I've described could be helpful for debugging and unit testing in development as well as useful for QA and quality control procedures. I welcome your suggestions on additional uses for the solution as well as feedback on how it worked for you.



ARTICLE TOOLS

Comments
  • Eduardo
    1 month ago
    Apr 22, 2012

    Hi! Thank you for your post, it was really helpful... but I couldn't get it to work just as I wanted to. You see, I'm able to get to the variables, read them and change them, but as soon as I end the Task, it changes the variables back to their original values. I need to SAVE those changes... the idea is that the Task will be used to setup the Package, and the user will set Drive letter and a path. So I need to save those changes so they don't have to keep setting them each time they run it.
    Thanks for your feedback!

You must log on before posting a comment.

Are you a new visitor? Register Here