Developing a Custom Destination Component

Microsoft SQL Server 2005 Integration Services (SSIS) gives developers the ability to write custom destination components that can connect to and store data in any custom data source. Custom destination components are useful when you need to connect to data sources that cannot be accessed by using one of the existing source components included with Integration Services.

Destination components have one or more inputs and zero outputs. At design time, they create and configure connections and read column metadata from the external data source. During execution, they connect to their external data source and add rows that are received from the components upstream in the data flow to the external data source. If the external data source exists prior to execution of the component, the destination component must also ensure that the data types of the columns that the component receives match the data types of the columns at the external data source.

Implementing the design-time functionality of a destination component involves specifying a connection to an external data source and validating that the component has been correctly configured. By definition, a destination component has one input and possibly one error output.

Creating the Component

Destination components connect to external data sources by using ConnectionManager objects defined in a package. The destination component indicates its requirement for a connection manager to the SSIS Designer, and to users of the component, by adding an element to the RuntimeConnectionCollection collection of the ComponentMetaData. This collection serves two purposes: first, it advertises the need for a connection manager to SSIS Designer; then, after the user has selected or created a connection manager, it holds a reference to the connection manager in the package that is being used by the component. When an IDTSRuntimeConnection90 is added to the collection, the Advanced Editor displays the Connection Properties tab, to prompt the user to select or create a connection in the package for use by the component.

The following code sample shows an implementation of ProvideComponentProperties that adds an input, and then adds a IDTSRuntimeConnection90 object to the RuntimeConnectionCollection.

Connecting to an External Data Source

After a connection is added to the RuntimeConnectionCollection, you override the AcquireConnections method to establish a connection to the external data source. This method is called at design time and at run time. The component must establish a connection to the connection manager specified by the run-time connection, and subsequently, to the external data source. Once established, the component should cache the connection internally and release it when ReleaseConnections is called. Developers override this method, and release the connection established by the component during AcquireConnections. Both of these methods, ReleaseConnections and AcquireConnections, are called at design time and at run time.

The following code example shows a component that connects to an ADO.NET connection in the AcquireConnections method, and then closes the connection in ReleaseConnections.

Validating the Component

Destination component developers should perform validation as described in Component Validation. In addition, they should verify that the data type properties of the columns defined in the component's input column collection match the columns at the external data source. At times, verifying the input columns against the external data source can be impossible or undesirable, such as when the component or the SSIS Designer is in a disconnected state, or when round trips to the server are not acceptable. In these situations, the columns in the input column collection can still be validated by using the ExternalMetadataColumnCollection of the input object.

This collection exists on both input and output objects and must be populated by the component developer from the columns at the external data source. This collection can be used to validate the input columns when the SSIS Designer is offline, when the component is disconnected, or when the ValidateExternalMetadata property is false.

The following sample code adds an external metadata column based on an existing input column.

During execution, the destination component receives a call to the ProcessInput method each time a full PipelineBuffer is available from the upstream component. This method is called repeatedly until there are no more buffers available and the EndOfRowset property is true. During this method, destination components read columns and rows in the buffer, and add them to the external data source.

Locating Columns in the Buffer

The input buffer for a component contains all the columns defined in the output column collections of the components upstream from the component in the graph. For example, if a source component provides three columns in its output, and the next component adds an additional output column, the buffer provided to the destination component contains four columns, even if the destination component will write only two columns.

The order of the columns in the input buffer is not defined by the index of the column in the input column collection of the destination component. Columns can be reliably located in a buffer row only by using the FindColumnByLineageID method of the BufferManager. This method locates the column that has the specified lineage ID in the specified buffer, and returns its location in the row. The indexes of the input columns are typically located during the PreExecute method, and cached by the developer for use later during ProcessInput.

The following code example finds the location of the input columns in the buffer during PreExecute and stores them in an array. The array is subsequently used during ProcessInput to read the values of the columns in the buffer.

The following sample shows a simple destination component that uses a File connection manager to save binary data from the data flow into files. This sample does not demonstrate all the methods and functionality discussed in this topic. It demonstrates the important methods that every custom destination component must override, but does not contain code for design-time validation. For a more complete sample destination component, see the DatasetDestination Component Sample.