Export (0) Print
Expand All

Developing a Custom Destination Component

Microsoft SQL Server Integration Services 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.

This section discusses the details of how to develop destination components, and provides code examples to clarify important concepts. For a general overview of data flow component development, see Developing a Custom Data Flow Component.

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 IDTSRuntimeConnection100 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 IDTSRuntimeConnection100 object to the RuntimeConnectionCollection.

using System;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime;

namespace Microsoft.Samples.SqlServer.Dts
{
    [DtsPipelineComponent(DisplayName = "Destination Component",ComponentType =ComponentType.DestinationAdapter)]
    public class DestinationComponent : PipelineComponent 
    {
        public override void ProvideComponentProperties()
        {
            // Reset the component.
            base.RemoveAllInputsOutputsAndCustomProperties();
            ComponentMetaData.RuntimeConnectionCollection.RemoveAll();

            IDTSInput100 input = ComponentMetaData.InputCollection.New();
            input.Name = "Input";

            IDTSRuntimeConnection100 connection = ComponentMetaData.RuntimeConnectionCollection.New();
            connection.Name = "ADO.net";
        }
    }
}

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.

using Microsoft.SqlServer.Dts.Runtime.Wrapper;

private SqlConnection sqlConnection;

public override void AcquireConnections(object transaction)
{
    if (ComponentMetaData.RuntimeConnectionCollection[0].ConnectionManager != null)
    {
        ConnectionManager cm = Microsoft.SqlServer.Dts.Runtime.DtsConvert.GetWrapper(ComponentMetaData.RuntimeConnectionCollection[0].ConnectionManager);
        ConnectionManagerAdoNet cmado = cm.InnerObject as ConnectionManagerAdoNet;

        if (cmado == null)
            throw new Exception("The ConnectionManager " + cm.Name + " is not an ADO.NET connection.");

        sqlConnection = cmado.AcquireConnection(transaction) as SqlConnection;
        sqlConnection.Open();
    }
}

public override void ReleaseConnections()
{
    if (sqlConnection != null && sqlConnection.State != ConnectionState.Closed)
        sqlConnection.Close();
}

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.

private void AddExternalMetaDataColumn(IDTSInput100 input,IDTSInputColumn100 inputColumn)
{
    // Set the properties of the external metadata column.
    IDTSExternalMetadataColumn100 externalColumn = input.ExternalMetadataColumnCollection.New();
    externalColumn.Name = inputColumn.Name;
    externalColumn.Precision = inputColumn.Precision;
    externalColumn.Length = inputColumn.Length;
    externalColumn.DataType = inputColumn.DataType;
    externalColumn.Scale = inputColumn.Scale;

    // Map the external column to the input column.
    inputColumn.ExternalMetadataColumnID = externalColumn.ID;
}

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 data flow. 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.

int[] cols;

public override void PreExecute()
{
    IDTSInput100 input = ComponentMetaData.InputCollection[0];

    cols = new int[input.InputColumnCollection.Count];

    for (int x = 0; x < input.InputColumnCollection.Count; x++)
    {
        cols[x] = BufferManager.FindColumnByLineageID(input.Buffer, input.InputColumnCollection[x].LineageID);
    }
}

Processing Rows

Once the input columns have been located in the buffer, they can be read and written to the external data source.

While the destination component writes rows to the external data source, you may want to update the "Rows read" or "BLOB bytes read" performance counters by calling the IncrementPipelinePerfCounter method. For more information, see Performance Counters.

The following example shows a component that reads rows from the buffer provided in ProcessInput. The indexes of the columns in the buffer were located during PreExecute in the preceding code example.

public override void ProcessInput(int inputID, PipelineBuffer buffer)
{
        while (buffer.NextRow())
        {
            foreach (int col in cols)
            {
                if (!buffer.IsNull(col))
                {
                    //  TODO: Read the column data.
                }
            }
        }
}

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.

using System;
using System.IO;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

namespace BlobDst
{
  [DtsPipelineComponent(DisplayName = "BLOB Extractor Destination", Description = "Writes values of BLOB columns to files")]
  public class BlobDst : PipelineComponent
  {
    string m_DestDir;
    int m_FileNameColumnIndex = -1;
    int m_BlobColumnIndex = -1;

    public override void ProvideComponentProperties()
    {
      IDTSInput100 input = ComponentMetaData.InputCollection.New();
      input.Name = "BLOB Extractor Destination Input";
      input.HasSideEffects = true;

      IDTSRuntimeConnection100 conn = ComponentMetaData.RuntimeConnectionCollection.New();
      conn.Name = "FileConnection";
    }

    public override void AcquireConnections(object transaction)
    {
      IDTSRuntimeConnection100 conn = ComponentMetaData.RuntimeConnectionCollection[0];
      m_DestDir = (string)conn.ConnectionManager.AcquireConnection(null);

      if (m_DestDir.Length > 0 && m_DestDir[m_DestDir.Length - 1] != '\\')
        m_DestDir += "\\";
    }

    public override IDTSInputColumn100 SetUsageType(int inputID, IDTSVirtualInput100 virtualInput, int lineageID, DTSUsageType usageType)
    {
      IDTSInputColumn100 inputColumn = base.SetUsageType(inputID, virtualInput, lineageID, usageType);
      IDTSCustomProperty100 custProp;

      custProp = inputColumn.CustomPropertyCollection.New();
      custProp.Name = "IsFileName";
      custProp.Value = (object)false;

      custProp = inputColumn.CustomPropertyCollection.New();
      custProp.Name = "IsBLOB";
      custProp.Value = (object)false;

      return inputColumn;
    }

    public override void PreExecute()
    {
      IDTSInput100 input = ComponentMetaData.InputCollection[0];
      IDTSInputColumnCollection100 inputColumns = input.InputColumnCollection;
      IDTSCustomProperty100 custProp;

      foreach (IDTSInputColumn100 column in inputColumns)
      {
        custProp = column.CustomPropertyCollection["IsFileName"];
        if ((bool)custProp.Value == true)
        {
          m_FileNameColumnIndex = (int)BufferManager.FindColumnByLineageID(input.Buffer, column.LineageID);
        }

        custProp = column.CustomPropertyCollection["IsBLOB"];
        if ((bool)custProp.Value == true)
        {
          m_BlobColumnIndex = (int)BufferManager.FindColumnByLineageID(input.Buffer, column.LineageID);
        }
      }
    }

    public override void ProcessInput(int inputID, PipelineBuffer buffer)
    {
      while (buffer.NextRow())
      {
        string strFileName = buffer.GetString(m_FileNameColumnIndex);
        int blobLength = (int)buffer.GetBlobLength(m_BlobColumnIndex);
        byte[] blobData = buffer.GetBlobData(m_BlobColumnIndex, 0, blobLength);

        strFileName = TranslateFileName(strFileName);

        // Make sure directory exists before creating file.
        FileInfo fi = new FileInfo(strFileName);
        if (!fi.Directory.Exists)
          fi.Directory.Create();

        // Write the data to the file.
        FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None);
        fs.Write(blobData, 0, blobLength);
        fs.Close();
      }
    }

    private string TranslateFileName(string fileName)
    {
      if (fileName.Length > 2 && fileName[1] == ':')
        return m_DestDir + fileName.Substring(3, fileName.Length - 3);
      else
        return m_DestDir + fileName;
    }
  }
}
Integration Services icon (small) Stay Up to Date with Integration Services

For the latest downloads, articles, samples, and videos from Microsoft, as well as selected solutions from the community, visit the Integration Services page on MSDN:


For automatic notification of these updates, subscribe to the RSS feeds available on the page.

Community Additions

ADD
Show:
© 2014 Microsoft