SSIS - How to use "Script Component As Transformation" ( InitCap In SSIS Package)

Scenario:

Let’s say we are extracting data from flat file. One of the column in source is Country Name, the data in this column is all mixed (lower case, upper case, lower upper mixed). Our goal is to produce output column with first letter of each word in uppercase, all other letters in lowercase (InitCap)

Solution:

To achieve this we can use Derived column transformation when we have one word or two words but when we have more than two words in string, it get complicated and messy writing expression in Derived column. We can use script component in Data flow task. The Script Component can be used as Source, Destination or Transformation. In our example we are going to use Script Component as transformation.

Here is the data that I used in flat file.
CountryName,SalePersonName,SaleAmount
uSA,aamir shahzad,100
united state of AMerica,andy,200
UsA,Mike,500
brazil,Sara,1000
INdia,Neha,200
Brazil,Barbra,200

Step 1: 
Open notepad and past above data. Save your file with Any name you like. In SSIS Package make connection to your flat file. Once connection is created use Flat file Source as your source.

Step 2: 
Bring Script Component from SSIS Toolbox to Data Flow Pane.Once Script Component will be placed, A window will automatically open for you to choose either you want to use Script Component as Source, Destination or Transformation. Choose Transformation for this example.
How to use Script Component as Transformation in SSIS Package for InitCap 


Step 3: 
Connect your Flat file Source to Script Component. Double click on Script Component. Choose " Input Columns" on left and then choose the column/s which you want to use in Script Component. In our case we are only using one column " CountryName".
How to use Flat File Source in Script Component in SSIS Package

Step 4: 
Click on "Inputs and Outputs" on left and then add a new column under Output Columns. In our case we added a new column with name OutCountryName. After adding column, choose the correct data type for that column in Common properties pane as shown below.
Create new Column in Script Component in SSIS Package

Step 5:
Click on "Connection Managers" on left and then add connection as shown in below 
Real time example to use Script Component in SSIS Package to InitCap data from Flat File Source


Step 6: 
After adding the connection manager in Script component we are all set to write script. We can use Microsoft Visual C# 2010 OR Microsoft Visual Basic 2010. Click on "Script" on Left and then Click on "Edit Script" button as shown below
How to use C# script in Script Component in SSIS Package for InitCap

Step 7:
Here is the code that we wrote to Create new column OutCountryName with InitiCap. Most of the code is auto generated by Script Component.The only text highlighted in orange is written by me.

#region Help:  Introduction to the Script Component
/* The Script Component allows you to perform virtually any operation that can be accomplished in
 * a .Net application within the context of an Integration Services data flow.
 *
 * Expand the other regions which have "Help" prefixes for examples of specific ways to use
 * Integration Services features within this script component. */
#endregion

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
#endregion

///

/// This is the class to which to add your code.  Do not change the name, attributes, or parent
/// of this class.
///
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    #region Help:  Using Integration Services variables and parameters
    /* To use a variable in this script, first ensure that the variable has been added to
     * either the list contained in the ReadOnlyVariables property or the list contained in
     * the ReadWriteVariables property of this script component, according to whether or not your
     * code needs to write into the variable.  To do so, save this script, close this instance of
     * Visual Studio, and update the ReadOnlyVariables and ReadWriteVariables properties in the
     * Script Transformation Editor window.
     * To use a parameter in this script, follow the same steps. Parameters are always read-only.
     *
     * Example of reading from a variable or parameter:
     *  DateTime startTime = Variables.MyStartTime;
     *
     * Example of writing to a variable:
     *  Variables.myStringVariable = "new value";
     */
    #endregion

    #region Help:  Using Integration Services Connnection Managers
    /* Some types of connection managers can be used in this script component.  See the help topic
     * "Working with Connection Managers Programatically" for details.
     *
     * To use a connection manager in this script, first ensure that the connection manager has
     * been added to either the list of connection managers on the Connection Managers page of the
     * script component editor.  To add the connection manager, save this script, close this instance of
     * Visual Studio, and add the Connection Manager to the list.
     *
     * If the component needs to hold a connection open while processing rows, override the
     * AcquireConnections and ReleaseConnections methods.
     *
     * Example of using an ADO.Net connection manager to acquire a SqlConnection:
     *  object rawConnection = Connections.SalesDB.AcquireConnection(transaction);
     *  SqlConnection salesDBConn = (SqlConnection)rawConnection;
     *
     * Example of using a File connection manager to acquire a file path:
     *  object rawConnection = Connections.Prices_zip.AcquireConnection(transaction);
     *  string filePath = (string)rawConnection;
     *
     * Example of releasing a connection manager:
     *  Connections.SalesDB.ReleaseConnection(rawConnection);
     */
    #endregion

    #region Help:  Firing Integration Services Events
    /* This script component can fire events.
     *
     * Example of firing an error event:
     *  ComponentMetaData.FireError(10, "Process Values", "Bad value", "", 0, out cancel);
     *
     * Example of firing an information event:
     *  ComponentMetaData.FireInformation(10, "Process Values", "Processing has started", "", 0, fireAgain);
     *
     * Example of firing a warning event:
     *  ComponentMetaData.FireWarning(10, "Process Values", "No rows were received", "", 0);
     */
    #endregion

    ///

    /// This method is called once, before rows begin to be processed in the data flow.
    ///
    /// You can remove this method if you don't need to do anything here.
    ///
    public override void PreExecute()
    {
        base.PreExecute();
        /*
         * Add your code here
         */
    }

    ///

    /// This method is called after all the rows have passed through this component.
    ///
    /// You can delete this method if you don't need to do anything here.
    ///
    public override void PostExecute()
    {
        base.PostExecute();
        /*
         * Add your code here
         */
    }

    ///

    /// This method is called once for every row that passes through the component from Input0.
    ///
    /// Example of reading a value from a column in the the row:
    ///  string zipCode = Row.ZipCode
    ///
    /// Example of writing a value to a column in the row:
    ///  Row.ZipCode = zipCode
    ///
    /// The row that is currently passing through the component
    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        /*
         * This is the only Code we have written. We created Function InitCapString that we used on
         * Input Column CountryName and Created New Column OutCountryName
         */

        Row.OutCountryName =InitCapString(Row.CountryName);
    }

    private string InitCapString(string p)
    {
        return new
System.Globalization.CultureInfo("en").TextInfo.ToTitleCase(p.ToLower());
        throw new NotImplementedException();
    }


    }

Final Output
As we can see in below snapshot that all the column values for CountryName are formatted to InitiCap ( First Upper Case and rest lowercase).
How to InitCap String data in SSIS Package by using Script Component