Thursday, October 8, 2009

How to run a DTSX Programmatically through C#

Hey there...

For a data integration service, I create four DTSX packages. These were generic packages which needs to be executed one after the another. I tried using SQL Server Agent to schedule it as a JOB which has four steps to be executed sequentially. Unfortunately the package which is working fine is visual studio is not working when scheduled. There seems to be some permission issues(had the same issue few days before). As i had users waiting the see the output. I have to find an alternative solution soon. Here is the C# code to run the DTSX from a EXEC file.


Code:

Package pkg;

Application app;

DTSExecResult pkgResults;

Variables varStore;


pkgLocation = @"c:\documents and settings\Path\Filename.dtsx";

app = new Application();

pkg = app.LoadPackage(pkgLocation, null);

varStore = pkg.Variables;

varStore[YourVariable].Value = 123;

pkgResults = pkg.Execute();


Required NameSpace: The reference of Microsoft.SQLServer.ManagedDTS.dll has to be added and in code -

using Microsoft.SqlServer.Dts.Runtime;


Hope it helps :)


No comments: