Purpose: Fetch all the user variables that are in the SSIS package and write the variable name and their values in a SQL Server 2008 table.
What have I tried: I got a small “Script Task” working to Display the variable name and their value. The script is as follows.
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
namespace ST_81ec2398155247148a7dad513f3be99d.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
Package pkg = app.LoadPackage(
@"C:\package.dtsx",
null);
Variables pkgVars = pkg.Variables;
foreach (Variable pkgVar in pkgVars)
{
if (pkgVar.Namespace.ToString() == "User")
{
MessageBox.Show(pkgVar.Name);
MessageBox.Show(pkgVar.Value.ToString());
}
}
Console.Read();
}
}
}
What needs to be done: I need to take this and dump the values to a database table. I am trying to work on a script component for this, but due to lack of knowledge of .net scripting, I havent gotten anywhere close to the finish line. This is what I have done on the component side.
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using Microsoft.SqlServer.Dts;
using System.Windows.Forms;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void CreateNewOutputRows()
{
#region Class Variables
IDTSVariables100 variables;
#endregion
variables = null;
this.VariableDispenser.GetVariables(out variables);
foreach(Variable myVar in variables)
{
MessageBox.Show(myVar.Value.ToString());
if (myVar.Namespace == "User")
{
Output0Buffer.ColumnName = myVar.Value.ToString();
}
}
}
}
I can think of 2 ways of solving this issue right now .
Table Script for inserting the variable name and value is
1.Use
Script taskand write the below codeExplanation:In this ,i’m creating a class which has properties Name and Val . Retrieve the package variables and their value using your code and store them in a collection
List<SSISVariable>.Then pass this collection to a method (InsertIntoTable) which simply inserts the value into the database by enumerating through the collection .Note :
2.Using ForEach Loop
Design
Step 1: create a Variable
VariableCollectionwhich is of typeSystem.Objectand anothervariable
Itemwhich is of typeStringto store the result from Foreach loopStep 2: For the 1st script task .
VariableCollectionis used to store the Variable name and its valueStep 3: Inside the script task
Main Methodwrite the following codeStep 4: Drag a Foreach loop and in the expression use
Foreach from Variable EnumeratorStep 5:Foreach loop enumerates the value and stores it in a variable
User::ItemStep 6:Inside the foreach loop drag a script task and select the variable
step 7: Write the below code in the main method
Explanation : In the Foreach loop ,i can only enumerate one variable .So the logic is, i need to somehow pass the variable name and its value into one variable .And for this ,i have concatenated name and value
Script task in the foreach loop simply splits the variable and stores it into a array of string and then u can access the name and value using array index and store it in the database