Thursday, March 17, 2011

SSIS Script task(C# code)

#Declare the variable("myVariable") of string type for package level and update the variable value using the script task.
Follow the Steps:
Step1:Add the variable "myVariable"  in the script task editor under "ReadWriteVariables".Please Note that the variable name is case sensitive.
Step2:click on edit script:
Dts.Variables["myVariable"].Value = "hello world";
output: "hello world" will assign to the variable "my Variable"
****************************************************************
#Display the variable value in to the message box:
MessageBox.Show(Dts.Variables["myVariable"].Value.ToString());
output: It will show you the messagebox with"hello world".
*****************************************************************
#Get the substring  form the variable myVariable which contains "hello world" string.
MessageBox.Show(Dts.Variables["var2"].Value.ToString().Substring(0, 5););
output:It will display the messagebox with "hello" string.
******************************************************************
#How to pass parameters in the store procedure from the Script task?
Find the Code Below:
private void LogFileNameEx(string strFileName, bool bSuccess, string strMessage)
        {
OleDbConnection sqlConnection = new OleDbConnection(Dts.Connections["TFOWarehouse"].ConnectionString);
//TFO Warehouse is the connection manager

            using (OleDbCommand cmdUspFileProcessingDetailsOnBegin = new OleDbCommand("MD.usp_GetFileProcessingList"))
            {
                cmdUspFileProcessingDetailsOnBegin.Connection = sqlConnection;
                cmdUspFileProcessingDetailsOnBegin.CommandType = CommandType.StoredProcedure;
                cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pExecutionLogId", SqlDbType.Int).Value = Dts.Variables["LogID"].Value;
                cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pFileType", SqlDbType.VarChar).Value = Dts.Variables["FileNameTemplate"].Value;
                cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pFileName", SqlDbType.VarChar).Value = strFileName;
                cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pMessage", SqlDbType.VarChar).Value = strMessage;
                if (bSuccess)
                    cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pExtractDate", SqlDbType.DateTime).Value = dtFileExtract;
                else
                    cmdUspFileProcessingDetailsOnBegin.Parameters.Add("@pExtractDate", SqlDbType.DateTime).Value = DBNull.Value;
                try
                {
                    sqlConnection.Open();
                    cmdUspFileProcessingDetailsOnBegin.ExecuteNonQuery();
                }
                finally
                {
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                }
            }
        } 


The store procedure structure is below:



ALTER PROCEDURE [MD].[usp_GetFileProcessingList]    
 @pExecutionLogId  INT    
 ,@pFileType   VARCHAR(255)    
 ,@pFileName   VARCHAR(255)    
    ,@pMessage          VARCHAR(255) = NULL    
    ,@pExtractDate  DATETIME = NULL  
WITH EXECUTE AS CALLER    
AS    
BEGIN    
 SET NOCOUNT ON    
 IF @pExtractDate IS NU



The parameter name should be same.
******************************************************************************

No comments:

Post a Comment