Script Component Transformation in SSIS

This article shows you how to use Script Component in SSIS to transform a database column.

Assumption: You have at least Visual Studio 2013 with SQL Server Data Tool installed.

Scenario: Appending a “1” in all the product number and store in a new table.

1.      Create an Integration Services Project in Visual Studio 2015.

You can also use VS 2013

image

2.      Drag the Data Flow Task to the Control Flow.

image

3.      Double click the Data Flow Task. Drag the ADO.NET to Data Flow.

image

4.      Double click to data source to point to the database. In this example I used Northwind database.

image

image

image

5.      Drag the Script Component to Data Flow. Select Transformation then link the ADO.NET Source (blue color arrow) to it.

image

6.      Double click the Script Component to open the Script Transformation Editor.

7.      Create a Connection Managers. In this example, I name in DWConnection. You need to use this name in your code later.

 7

8.      In the Input Column I choose ProductID and ProductIDNew. I created another new column in the Products table. Realize two things here.

a.      The Usage Type for ProductIDNew is ReadWrite.

b.      The Input name is Input 0.

8

9.      Go to Script and click the Edit Script… button.

image

10.   Insert the code below.

#region Namespaces

using System;

using System.Data;

using System.Data.SqlClient;

using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

using Microsoft.SqlServer.Dts.Runtime.Wrapper;

#endregion

 

/// <summary>

/// This is the class to which to add your code.  Do not change the name, attributes, or parent

/// of this class.

/// </summary>

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]

public class ScriptMain : UserComponent

{

 

    IDTSConnectionManager100 connMgr;

    SqlConnection sqlConn;

    SqlCommand sqlCmd;

    SqlParameter sqlParam;

 

    public override void AcquireConnections(object Transaction)

    {

        connMgr = this.Connections.DWConnection;

        sqlConn = (SqlConnection)connMgr.AcquireConnection(null);

    }

 

    public override void PreExecute()

    {

        base.PreExecute();

        /*

         * Add your code here

         */

    }

 

    public override void PostExecute()

    {

        base.PostExecute();

        /*

         * Add your code here

         */

    }

 

    public override void Input0_ProcessInputRow(Input0Buffer Row)

    {

        /*

         * Add your code here

         */

        string strTemp = Row.ProductID.ToString();

        strTemp = strTemp + “1”;

        Row.ProductIDNew = int.Parse(strTemp);

    }

 

}

 

11.   Save and close the script editor.

12.   Drag ADO.NET Destination to Data Flow.

13.   Connect the Script Component to ADO.NET Destination.

14.   Configure the ADO.NET Destination to point to the destination table (for this example the Products2 table structure is the same as the Products1 tables, these are the tables that I created beforehand).

15.   Mapping the columns.

image

image

Now you can test the project (click Start).

 15

 16

Advertisements

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Community, Computers and Internet and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s