.NET King Logo Comments on: Some important tips passing variables to SSIS package programmatically
Skip Navigation Links
Home
Weblog
How to... (video)
Articles
About us
Contact us
0 Items in Video cart!

Some important tips passing variables to SSIS package programmatically (11/24/2007)

Have you ever tried to call an SSIS (SQL Server Integration Services) package programmatically? It's just a piece of cake. Hah, you haven't tried it yet? Ok, see this MSDN article. Hey guys I am not writing this weblog just to put the links that you can easily find them using any search engine. Here are my points.

I create a simple SSIS package with only one script task and two parameters and I want to call this package using C# code. I have already built this package and you can download it from here. The parameters are:

yourName (Int32) that has the value of "Alireza" already hard coded in the package
yourAge (String) that has the value of 35 and again this value is hard coded in the package

If I run this package it displayed a simple message box that shows "Hi Alireza you will be 36 next year". Now lets call this package programmatically:

Assuming that the package is loaded on "C:\SSISProgrammatically\Package.dtsx" this piece of code can run the package successfully. (don't forget to add the Microsoft.SQLServer.ManagedDTS to your project references)

string pkgLocation;
Package pkg;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
pkgLocation =
@"C:\SSISProgrammatically\Package.dtsx";
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);
pkgResults = pkg.Execute();

By now it is nothing more than the MSDN example. Here are my points:

We want to pass the variables programmatically to SSIS Package. Here is how:

string pkgLocation;
Package pkg;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
pkgLocation =
@"C:\SSISProgrammatically\Package.dtsx";
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);

pkg.Variables["yourName"].Value = "Dennis MacAlistair Ritchie";
pkg.Variables["yourAge"].Value = 66;


pkgResults = pkg.Execute();

OK, It is quite easy but watch out for the  data types. How do you like this example:

 string pkgLocation;
Package pkg;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
pkgLocation =
@"C:\SSISProgrammatically\Package.dtsx";
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);

pkg.Variables["yourName"].Value = "Dennis MacAlistair Ritchie";
pkg.Variables["yourAge"].Value = "Ha ha!";


pkgResults = pkg.Execute();

You will end up to a bizarre execution result. It compiles successfully and runs successfully and gives you no execution result. You can simply guess that "Ha ha!" cannot be converted to integer, but where can you track this error? Server Application log? Good guess, but this is what you find in the log:"Package "Package" failed." How much it can help I really don't know but here is my advice.

NEVER RELY ON THE IMPLICIT DATA CONVERSION. Check all your variables in the SSIS package in advance and make sure all the values that you pass to SSIS variables are exactly the same data types as the variables inside the package.

Cheers
Alireza


By: chris  
Realize this is pretty old but while searching came across this page. Using Powershell to run SSIS packages manually and ran into this same problem. The variable would always get passed as a string. What I found is if you pass them as the correct datatype, then they'll be get roped in properly. In this case I had to assign it as a posh variable in the correct datatype. Example:
if ($datatype -eq 'String') {[string]$value = Read-Host "Set a value for variable < $varName >"}
elseif ($datatype -eq 'Int32') {[int]$value = Read-Host "Set a value for variable < $varName >"}
elseif ($datatype -eq 'Boolean') {[Boolean]$value = Read-Host "Set a value for variable < $varName >"}
elseif ($datatype -eq 'DateTime') {[datetime]$value = Read-Host "Set a value for variable < $varName >"}
$package.Variables["$varName"].Value = $value
}
In this case I'm running through the user::variables in a foreach and determining the datatype. Hope this helps anyone else that runs into this.
URL:  
By: Alireza  
Hi Pierre Godard
You are executing the package the moment that you run this line in C# code:
pkgResults = pkg.Execute();

If you have any default values for those variables they are overridden by the C# code.
URL:  
By: Azar Ali Zain  
Dude, this really helped!! I spent my whole night figuring out how to pass parameter, and due to the wrong library, DTSRuntimeWrap.dll reference isntead of ManagedDTS.dll, I just lost all my time hittingm y head on the wall!! But eventually your post helped, thanks alot mate!
Cheers!
URL: tech-azaralizain@blogspot.com  
By: Pierre Godard  
I would like to use the code posted here. My questions is: I am using a SQLServer Config for my connections and I noticed that some of my variables are defined in the config file also. I would prefer to pass the variables value during package execution. Would there be any conflict with config and exec var passing?
URL:  
By: Alireza  11/29/2007
That's the bad part of it. The package and the client application are running in two seperate processes and exceptions are not returned to the client.
URL: http://www.dotnetking.com  
By: Mostafa Arafa  11/29/2007
Good Tip... BUT Did you try to enclose your code in try..catch block to get a dtailed error message ?
URL: http://moustafa-arafa.blogspot.com  
Your name:
Email:
URL:
Comments:
 

 -