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.RuntimePublic 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.EventArgsPublic 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.