Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

08 Mar 2011

Get/SetVariable value from SSIS VariableDispenser

Here is some code that allows you to Get/Set a variable (object) value from/on the VariableDispenser in an SSIS package:

public static T GetVariable<T>(this VariableDispenser variableDispenser, string scopedVariableName)
{
  Variables variables = null;

  try
  {
    variableDispenser.LockForRead(scopedVariableName);
    variableDispenser.GetVariables(ref variables);
    return (T)variables[0].Value;
  }
  finally
  {
    if (variables != null) variables.Unlock();
  }
}

public static void SetVariable<T>(this VariableDispenser variableDispenser, string scopedVariableName, T value)
{
  Variables variables = null;

  try   
  {  
    variableDispenser.LockForWrite(scopedVariableName);  
    variableDispenser.GetVariables(ref variables);  
    variables[0].Value = value;  
  }  
  finally  
  {   
    if (variables != null) variables.Unlock();  
  }
}