February 4th, 2009, 12:52 AM
Points: 17,437, Level: 57
Join Date: May 2003
Location: Indianapolis, IN, USA.
Thanked 128 Times in 97 Posts
Article: Script Task (.NET) in SQL Server 2008 Integration Services (SSIS)
This article is exerpted from chapter 3 SSIS Tasks of the Wrox book Professional Microsoft SQL Server 2008 Integration Services by
by Brian Knight, Erik Veerman, Grant Dickinson, Douglas Hinson, Darren Herbold and is reused by permission of the publisher. This may not be reused without publisher permission
Script Task (.NET)
The Script Task allows you to access the Microsoft Visual Studio Tools for Applications (VSTA) environment to write and execute scripts using the VB and C# languages. The VSTA environment is new in the latest version of SSIS and replaces the Visual Studio for Applications (VSA) environment from the 2005 version. Scripting now is almost a misnomer because the latest SSIS edition solidifies the connection to the full .NET libraries for both VB and C#. The latest addition to SSIS of the VSTA environment and the Script Task in general also offer these extra functional advantages:
The Script Task is configured through the Script tab in the Script Task Editor (shown in Figure 3-6). The ScriptLanguage property is where you select the .NET language you’d like to use in the task. Notice that the default language is set to C#, so if you are coding in VB.NET, don’t whiz through these settings when setting up your Script Tasks. If you have done any SSIS development with the previous versions, you’ll also notice that the PreCompileScriptIntoBinaryCode property has been permanently removed. The result is that all scripting code will automatically compile and persist into the package. This speeds up and reduces runtime errors in the task significantly.
The EntryPoint property allows you to provide an alternative function to call initially when the ScriptMain class is instantiated. Typically, you’ll leave this set to the default Main() function. The ReadOnlyVariables and ReadWriteVariables properties allow you to pass SSIS variables into the script as a listing of variable names separated by commas. The typing out of the variable names was a little unconventional in the earlier versions of SSIS, but the latest version provides the capability of browsing to the variable collection and selecting the variables. Having these variables provides a significant advantage when coding. You only need to refer to them by ordinal position or by name in the Variable collection to be able to access their values without worrying about locking, unlocking, or blocking variables during read and write actions. Just make sure you have the variables you wish to write back to in the ReadWriteVariables property, or you’ll get an error in the script. There are also alternative methods for altering variables that aren’t provided in these collections during set up that are demonstrated in Chapter 9 on scripting.
When you click the actionable Edit Script button, the Visual Studio Tools for Applications environment opens to allow coding directly in the class ScriptMain. In this IDE, you have access to all the advanced debugging tactics, breakpoints, and IntelliSense found in the Visual Studio environment. If you create a package with a variable named myValue containing the string "Hello World", and set up the Script Task like Figure 3-7, the following example shows you how to write code that uses the passed-in myValue variable:
- A coding environment with the advantage of IntelliSense
- An integrated Visual Studio design environment within SSIS
- An easy-to-use methodology for passing parameters into the script
- The ability to add breakpoints into your code for testing and debugging purposes (for only one Script Task per package)
- The automatic compiling of your script into binary format for a speed advantage (This was configurable in earlier releases of SSIS.)
public void Main()
System.Windows.Forms.MessageBox.Show("MyValue=" + Dts.Variables
Dts.TaskResult = (int)ScriptResults.Success;
First, the script checks for the existence of the variable, and then pops up a message box with the famous message “Hello World” as you see in Figure 3-7.
This is just a very simple example of the Script Task in action. We’ve created an entire chapter to dive into the details and specific use cases for both the Script Task and the Data Flow version called the Script Component, so go read Chapter 9 for more information.
Public Sub Main()
If Dts.Variables.Contains(“User::MyValue”) = True Then
System.Windows.Forms.MessageBox.Show("myValue=" & Dts.Variables
Dts.TaskResult = ScriptResults.Success