Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access ssis package variables inside script component

How can I access variables inside my C# code which I've used in Data Flow -> Script Component - > My c# Script with my SSIS package?

I have tried with which is also not working

IDTSVariables100 varCollection = null; this.VariableDispenser.LockForRead("User::FilePath"); string XlsFile;  XlsFile = varCollection["User::FilePath"].Value.ToString(); 
like image 709
Ashfaque Ali Solangi Avatar asked Nov 19 '12 08:11

Ashfaque Ali Solangi


People also ask

How do you use variables in a component script?

You can make existing variables available for read-only or read/write access by your custom script by entering comma-delimited lists of variables in the ReadOnlyVariables and ReadWriteVariables fields on the Script page of the Script Transformation Editor. Keep in mind that variable names are case-sensitive.

How do I use variables in script task in SSIS?

First you need to add the variables and/or parameters to the readonly and/or readwrite variables. Edit the Script Task and in the Script Pane you will find two textboxes: ReadOnlyVariables and ReadWriteVariables. They are for variables and parameters. Note: parameters are always readonly.

How do you pass a variable to a script component in SSIS?

To use a variable in a 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 task, according to whether or not your code needs to write to the variable. Show activity on this post.

How do I view variables in SSIS package?

By default, the Variables window is located below the Connection Managers area in the SSIS Designer, in SQL Server Data Tools (SSDT). If you don't see the Variables window, click Variables on the SSIS menu to display the window. You can optionally display the Variables window by mapping the View.


2 Answers

Accessing package variables in a Script Component (of a Data Flow Task) is not the same as accessing package variables in a Script Task. For a Script Component, you first need to open the Script Transformation Editor (right-click on the component and select "Edit..."). In the Custom Properties section of the Script tab, you can enter (or select) the properties you want to make available to the script, either on a read-only or read-write basis: screenshot of Script Transformation Editor properties page Then, within the script itself, the variables will be available as strongly-typed properties of the Variables object:

// Modify as necessary public override void PreExecute() {     base.PreExecute();     string thePath = Variables.FilePath;     // Do something ... }  public override void PostExecute() {     base.PostExecute();     string theNewValue = "";     // Do something to figure out the new value...     Variables.FilePath = theNewValue; }  public override void Input0_ProcessInputRow(Input0Buffer Row) {     string thePath = Variables.FilePath;     // Do whatever needs doing here ... } 

One important caveat: if you need to write to a package variable, you can only do so in the PostExecute() method.

Regarding the code snippet:

IDTSVariables100 varCollection = null; this.VariableDispenser.LockForRead("User::FilePath"); string XlsFile;  XlsFile = varCollection["User::FilePath"].Value.ToString(); 

varCollection is initialized to null and never set to a valid value. Thus, any attempt to dereference it will fail.

like image 119
Edmund Schweppe Avatar answered Sep 20 '22 19:09

Edmund Schweppe


First List the Variable that you want to use them in Script task at ReadOnlyVariables in the Script task editor and Edit the Script

To use your ReadOnlyVariables in script code

String codeVariable = Dts.Variables["User::VariableNameinSSIS"].Value.ToString(); 

this line of code will treat the ssis package variable as a string.

like image 31
Adithya Alapati Avatar answered Sep 18 '22 19:09

Adithya Alapati