Database Reference
In-Depth Information
It is possible to access variables within instances of the Script task or the Script
component even if you do not explicitly declare their use in the graphical settings edit-
or. Within the SSIS scripting toolset, you will find a couple of functions that will allow
you to programmatically lock and unlock variables for either read-only or read-write
access. As shown in Listing 3-6 , you can use the VariableDis-
penser.LockOneForRead() function to capture the value of a variable that was
not previously declared.
Listing 3-6 . Manually Lock a Variable
// Programmatically lock the variable we need
Variable vars = null;
Dts.VariableDispenser.LockOneForRead("RunID", ref vars);
// Assign to script variable
runID = int.Parse(vars["RunID"].Value.ToString());
// Unlock the variable object
vars.Unlock();
Using a method similar to the one shown in Listing 3-6 , you can manipulate vari-
able values by using the function VariableDispenser.LockOneForWrite() ,
which would allow you to write to as well as read from the variable value.
Variable Data Types
As you may have derived from Listing 3-4 and Listing 3-5 , the interpreted data type for
variable values will differ between the Script task and the Script component. With the
latter, any variable that you declare in the graphical settings editor will surface as the
.NET data type equivalent of the SSIS variable type, and there is no need to perform a
type cast. When using the Script task (and the Script component, if you opt to use
either the LockOneForRead() or LockOneForWrite() method), variables are
presented as the generic Object type, and most of the time you'll have to cast to an
appropriate type any variable used in script code. As shown in Figure 3-10 , you'll get a
compiler error if you forget to cast a variable value to the appropriate type.
 
 
 
Search WWH ::




Custom Search