Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.
Guess what! you can execute DTS Packages from C# code. Yeah I know its pretty cool. Take a look at the code below. Don't try to run this code as you need to do lot of modifications. Check out the link I have given at the bottom of this posting.

// This method will run the DTS Package
    private static void RunDTSPackage()
    {              
        // Name of the package to run
        string packageName = "AzamSharpDTSTesting";
        object pVarPersistStgOfHost = null; 

        DTS.PackageClass package = new DTS.PackageClass();
        package.LoadFromSQLServer("localhost","sa","azam123", DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection
        , null, null, null, packageName, ref pVarPersistStgOfHost);

        try
        {
            // Execute the package
            Console.WriteLine("DTS Package Executing.."); 
            package.Execute();
            Console.WriteLine("DTS Package Completed");
           
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        finally
        {
            package.UnInitialize();
            package = null;
        }
        
    }

Check out this link: http://www.15seconds.com/issue/030909.htm Posted on Wednesday, October 19, 2005 8:29 PM | Back to top


Comments on this post: Executing DTS Packages using C#

# re: Executing DTS Packages using C#
Requesting Gravatar...
Hi, I run that code but nothing happened, everything seems to work fine but not only the DTS is never called but also any exception is thrown, I was not able to make it run properly, could you give me some tips

douglas.acosta@gmail.com
Left by Douglas Acosta on Jul 12, 2006 10:30 AM

# re: Executing DTS Packages using C#
Requesting Gravatar...
Adding DTS to your code is very powerful. To make this code run, you need to include the COM Component "Microsoft DTSPackage Object Library" to your references to be able to access the DTS object.
Left by cvantuss on Jul 31, 2006 1:20 PM

# re: Executing DTS Packages using C#
Requesting Gravatar...
I am seeing the same issue where it looks like it executes, but nothing happens. In debug, I can see the tasks on the package, so I know the code actually has an instance of it. I have added the COM component Microsot DTSPackage Object Library to the project and it acts the same before and after the added reference.

Any ideas.

Thanks.

Sean
Left by Sean on Aug 09, 2006 3:43 AM

# re: Executing DTS Packages using C#
Requesting Gravatar...
I tried the above code and worked fine!

Of course i changed the DTSSQLStgFlag_UseTrustedConnection
property, because i used username and password...
Left by Jilia on Aug 18, 2006 5:39 AM

# re: Executing DTS Packages using C#
Requesting Gravatar...
Hi,

I tried the above code to execute the DTS package. It's not throwing any error but DTS ain't running. Any help regarding this would be appreciated.
Left by Vinu Lakshman on Mar 05, 2007 11:19 PM

# re: Executing DTS Packages using C#
Requesting Gravatar...
Be careful if you have a SQL server connection in your DTS job that uses Windows authentication to connect. When you run the DTS job from Enterprise Manager it will work fine, but when you run it from code, it is running as whatever user is executing the code (which is the iusr/aspnet user in an ASP.NET app) which may not be able to open the SQL server connection within the DTS job.

If you are having a similar problem, try using SQL Server Authentication mode in your SQL server connections within your DTS job.
Left by Michael on May 08, 2007 11:41 AM

# re: Executing DTS Packages using C#
Requesting Gravatar...
Two other things regarding what I mentioned above:
1. I could see how this might also manifest itself in the form of denial of file permissions.
2. The DTS execution may not throw an exception if parts of the job fail.
Left by Michael on May 08, 2007 12:11 PM

# re: Executing DTS Packages using C#
Requesting Gravatar...
Please help:

I created a DTS that imports data from a DBase file and puts it in a table on my SQL2000 Server. (It is able to access the DBase file via a local DNS that I created in my AdministrativeTools->DataSource).

I wrote the following C# code that is able to call and execute the DTS.


object pVarPersistStgOfHost = null;
DTS.Package pkg = new DTS.Package();
pkg.LoadFromSQLServer("VServer","VUser","VP888",DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, null,null,null,"DTSUpdateDbaseFilet",ref pVarPersistStgOfHost);
pkg.Execute();
pkg.Steps.ToString();

It works for all my DTS that transform data within my database. This even works on the computer that everything is on. My problem is when I try to execute this package via an application that is on another computer. The data doesn't get imported. (Note that the connection string is fine and that other DTS that transform data within other tables in the database works fine when executing from the remote computer/application).
Left by MTech8 on May 14, 2007 1:42 PM

# re: Executing DTS Packages using C#
Requesting Gravatar...
How do i get DTS job failure in my c# Code?
Left by satish on Jan 25, 2008 9:48 AM

# re: Executing DTS Packages using C#
Requesting Gravatar...
next time if u write any code then add more specifically.
for example which references to and what are other dependencies and components required to run and excecute this coe.
this is just wasting of time...
this is useless code and no use.dont waste ppl's time
Left by vishal on Apr 24, 2008 5:48 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net