Search
Database

Using SQL Server 2005 Integration Services : Programming Integration Services (part 1) - Creating Packages Programmatically - Data Flow

Published 6 min readBy Programming4us Editorial Team
The learning curve is considerably steeper than for creating packages using the Import And Export Wizard and the designer. We advise you to become familiar with using the wizard and the designer to create packages before you start working with package creation and extension programmatically.

Programming in Visual Studio

Programming Integration Services in the Visual Studio environment is easy to set up. Depending on whether you’re building an extensibility object or a custom application, you can create either a Class Library project or an application project. Then you add references to the appropriate assembly and begin implementing your classes.

If you are building control flow extensibility object or a custom application that controls the Integration Service package, you must reference Microsoft.SqlServer.ManagedDTS. If you are building a data flow component, you must reference Microsoft.SqlServer.PipelineHost, Microsoft.SqlServer.DTSRuntimeWrap, and Microsoft.SqlServer.DTSPipelineWrap.

Loading and Executing Packages in Applications

You can load packages in applications by using the Integration Services Application object. This object returns information about the state of Integration Services (such as the data flow components that are installed) and performs other Integration Services–related tasks (such as determining which packages are running). One of the tasks supported by the object is the loading of packages. More than one method is available to do this—including LoadPackage and LoadFromSqlServer. The method you should use depends on how the package is stored. To create the Application object in managed code, you need to add a reference to Microsoft.SQLServer.ManagedDTS and use the namespace Microsoft.SqlServer.Dts.Runtime.

Listing 1 shows a managed code example that demonstrates package loading and execution.

Listing 1. Loading and executing a package

Creating Packages Programmatically

In addition to loading and executing packages programmatically, you can also use the Integration Services object model to create packages programmatically. The package’s control flow and data flow are conceptually different and therefore have different methods for their construction.

Control Flow

The programming model for control flow is rooted in the Package object. You create this object first and then create additional objects with the Package object’s Executables collection. If the object you create is a task, you get a task and you can set properties on it. If the object you create is a container, you get a container and you can set properties on the container and create child objects in the container’s Executables collection.

Once you have the executable objects in place, you can create precedence constraints by adding items to the PrecedenceConstraints collection.

Likewise, to create connections and variables, you add items to the Connections collection on the Package object and to the Variables collection on container objects.

Although you can create the entire package from scratch programmatically, one approach is to templatize the process as much as possible. Build most of the package interactively in SQL Server Business Intelligence Development Studio ahead of time, and then load the template package programmatically and modify it as needed until it has the correct composition. In this way, you minimize the amount of code you need to write and maximize ease of maintenance of your application because you can handle many types of changes simply by changing the template package; you don’t have to change a single line of code.

Data Flow

The data flow has its own object model for programmatically setting up the layout that describes a flow. For our example, we programmatically construct a package in C# containing a data flow that uses an OLE DB source adapter to read in data from a SQL Server 2005 table and then inserts the data into a destination table using the OLE DB destination adapter. The flow of the program is similar to how the Import And Export Wizard constructs its packages. Along the way, we review and introduce some concepts that programmers working with the data flow must know. To run the sample, you must have the AdventureWorks database available and the destination table available in tempdb (as shown in Listing 2).

Listing 2. SQL statement for the destination table used in the data flow programming sample

Listing 3 shows a complete data flow programming sample. When you build the sample, you must add references to:

Microsoft.SqlServer.ManagedDTS, Microsoft.SqlServer.DTSPipelineWrap, and Microsoft.SqlServer.DTSRuntimeWrap

Listing 3. Complete data flow programming sample