SSIS Script Task

Generally, Integration Services provides all the tasks required to perform daily or routine ETL operations. However, there are some situations where built-in tasks could not be more helpful. The SSIS Script Task allows one to implement functionalities that are not available or possible in the toolbox (both in built-in Tasks and transformations). The SSIS script task utilizes Microsoft VSTA (Visual Studio Tools for Applications) as the code environment where you can write the C# or VB code.

In Integration Services, the SSIS Script Task is the extended hand or functionality. It allows developers to integrate custom code into their ETL process or package. You can use famous languages like C# or Visual Basic.NET to write scripts within the package. 

SSIS Script Task Advantages

The following are some of the advantages of using SSIS Script Tasks:

  1. By writing custom code in C# or VB.NET, you can create a unique solution that fits your organization.
  2. Use Script tasks to implement business logic, and it can also handle complex transformations.
  3. It allows you to use external assemblies and libraries to perform advanced calculations or operations. However, you must add references to these dependencies in the Script Task project.
  4. The tasks editor window allows passing Read and Write variables from a package to the script. So, changing the package variable value can alter the script. 
  5. We can use SSIS Script Tasks in a package’s control and data flow (Script Component as Source, Destination, and Transformation).
  6. You can store the script task result in a variable to pass to the next task.
  7. If you want to perform row by row operations, use the script component; otherwise, use the script task.

TIP: SSIS Script Task VSTA provides all standard futures available in the general Visual Studio Environment.

SSIS Script Task to Send Email

Drag and drop the Script Task from the toolbox into the Control Flow region.

Drag Data Flow Task to Control Flow Region 1

Before we start configuring the SSIS Script task, let us create four variables. To create a variable, right-click on the control flow region, and it will open the context menu to select the variable option. Once you choose the variable option, it will open a new window called Variables to create the new variable.

Here, you can create n variables for the SSIS Script Task.

  • EmailFrom: Please specify the Email ID from which you want to send the Email.
  • EmailTo: Please specify the Email ID to whom you want to send the Email.
  • EmailSubject: Please specify the Subject you want to include
  • EmailBody: Please specify the Message. It can be Plain Text or HTML Message
Script Task in SSIS 2

Double click on the SSIS Script task will open the following editor to configure the components

  • EntryPoint: Please specify the Method name that the SSIS run-time calls as the entry point. The method name you specify here must be in the ScriptMain class. Remember, you can change the name as per your requirement, and when you change it, you have to change it in the ScriptMain class
Open Editor 3

SSIS Script Task General tab

Please click on the SSIS Script Task General tab to change the default Name and Description.

  • Name: Please provide the Unique Name.
  • Description: Briefly describe the Script Task Functionality. It is always a good practice to provide a valid description.
Add name and Description under General tab 4

ScriptLanguage: Microsoft uses two popular languages, Visual Basic (VB) and C#. I am familiar with C#, so we selected C# as the language.

Choose C# or Visual Basic Language 5
  • ReadOnlyVariables: Please select the variables you want to use in the SSIS Script Task, which may be user-defined or System default variables. Remember, variables selected as ReadOnlyVariables are used for the Read-only purpose (we can’t alter them)
  • ReadWriteVariables: Please select the variables you want to use in this task, which may be user-defined or System default variables. Remember, variables selected as ReadWriteVariables can be altered according to our requirements.

Please select the variables by clicking the Eclipse (…) button ReadOnlyVariables option.

Script Task in SSIS 6

Select the Variables window will open once you click the Eclipse (…) button. Please select the previously created variables.

Choose the Variables 7

Add C# code in SSIS Script Task.

Once you have selected the required variable, please click on the Edit Script.. button to write the actual C# Script

Script Task in SSIS 8

Once you click the Edit Task, SSIS will open the ScriptMain.cs class file to write the C# script.

To send an email from C#, we need to add two references or import references called using System.Net; and using System.Net.Mail;

TIP: If you are a VB developer, then it should be something like ScriptMain.vb

C# Code 9

Next, within the Main() function, add your Code. Remember, if your code is long or robust, try to divide the code by creating methods

Send Email using C# Code 10

C# code we used for this SSIS Script Task in the above screenshot is:

// C# code
String SendMailFrom = Dts.Variables["EmailFrom"].Value.ToString();
String SendMailTo = Dts.Variables["EmailTo"].Value.ToString();
String SendMailSubject = Dts.Variables["EmailSubject"].Value.ToString();
String SendMailBody = Dts.Variables["EmailBody"].Value.ToString();

try
  {
     MailMessage email = new MailMessage();
     SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
     // START
     email.From = new MailAddress(SendMailFrom);
     email.To.Add(SendMailTo);
     email.Subject = SendMailSubject;
     email.Body = SendMailBody;
     //END

     SmtpServer.Port = 587;
     SmtpServer.Credentials = new System.Net.NetworkCredential(SendMailFrom, "your password");
     SmtpServer.EnableSsl = true;

     SmtpServer.Send(email);
     MessageBox.Show("Email was Successfully Sent ");
  }
catch (Exception ex)
  {
      MessageBox.Show(ex.ToString());
  }
Dts.TaskResult = (int)ScriptResults.Success;

If you forgot to create Variables (or find it difficult to create variables), you can remove the first four lines of code and replace the code between //Start… END with the below-shown code

// Example
email.From = new MailAddress("xxxxxxxxxxxx@gmail.com");
email.To.Add("xxxxxxxxxxxxxy@gmail.com");
email.Subject = "Test Mail";
email.Body = "This Email is coming from SSIS Script Task";

Once you have finished editing it, Please close the ScriptMain.cs file and Script Task Editor. Let us run the package.

SSIS Script Task 11

From the above screenshot, you can observe that the SSIS Script Task is successful, and we got the message box saying that the Email was Successfully sent. Let me open my Gmail.

Gmail 12

From the above screenshot, you can see that we got the Email with the specified message.

Points to Remember :

  • If you want to email your Manager (or yourself) after every Data Load, you can use this SSIS Script and the Data Flow Task.
  • Always use the Variables to store emails, Passwords, Subjects, or SMTP credentials.
  • If there is an Error stating Secure Connection, then Go To Less Secure Apps in your Gmail account settings and Turn on the Access for Less Secure Apps option.

Comments are closed.