free geoip Executing a DTS Package From .NET - Jayson's Blog - jaysonKnight.com
jaysonKnight.com
Welcome to my corner of the internet

Executing a DTS Package From .NET

A discussion between some peers of mine came up the other day about executing a SQL Server DTS package from an ASP.NET web page.  The actual best practice is to invoke a stored procedure that in turn calls the DTS package you wish to run, however thanks to the simplicity of creating a RCW (runtime callable wrapper) via the VS.NET IDE on the legacy unmanaged Microsoft DSTPackage Object Library (located in your SQL Server installation directory in Tools\Binn\dtspkg.dll, also listed on the COM tab in the Add Reference dialog in the IDE), you can invoke a DTS package directly from code if you so wish.  Just as a refresher, adding an RCW from the IDE is accomplished by:

  1. Project references dialog (right click references, add reference)
  2. Find Microsoft DTSPackage Object Library in the exhaustive list of objects on the COM tab
  3. Select it, add it...voila.  The IDE automagically generates the correct interop assembly and places it in the bin folder, ready for instantiation

The actual code itself is straightforward, here is an example:

using System;

using System.Runtime.InteropServices;

using DTS;

 

namespace DTSPackage

{

      ///

      /// Summary description for Class1.

      ///

      class Class1

      {

            ///

            /// The main entry point for the application.

            ///

            [STAThread]

            static void Main(string[] args)

            {

                  Package2Class package = new Package2Class();

                  object pVarPersistStgOfHost = null;

                  try

                  {

                        package.LoadFromSQLServer("SERVER_NAME", // server name

                              null, // server username

                              null, // server password

                              DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection,

                              null, // if the package has a password, otherwise null

                              null, // probably null

                              null, // probably null

                              "PACKAGE_NAME",  // name of the DTS package

                              ref pVarPersistStgOfHost);

                        

                        package.Execute(); // execute the package

                  }

                  catch

                  {

                        throw;

                  }

                  finally

                  {

                        package.UnInitialize(); // unwrap the package

                        System.Runtime.InteropServices.Marshal.ReleaseComObject(package); // tell interop to release the reference

                  }

            }

      }

}

 

I still recommend calling DTS packages via stored procedures (or via a scheduled SQL Server job), but in a pinch, or for prototyping, testing, etc, the above code should work nicely.  I am assuming that with the release of Yukon, there will be a managed class for this, but you know what they say about assuming.


Posted May 29 2004, 05:46 PM by Jayson Knight

9 Comments

Jeff Cochand wrote re: Executing a DTS Package From .NET
on 07-22-2004 5:24 PM
Why do you say "The actual best practice is to invoke a stored rocedure that in turn calls the DTS package you wish to run"?

Isn't the above, pkg.LoadFromSQLServer & pkg.Execute, more direct? My problem with that I'm not able to get IIS / SQL Server login permissions correct, so pkg.Execute returns but doesn't execute anything. Do you know how to properly configure the IIS/SQL Server logins to get pkg.Execute to work?

Also, if you're not going to go the pkg.Execute route, why go thru the extra layer of a stored proc when calling dtsrun. Why not just shell out & call dtsrun from the C# code, rather than going thru the extra layer of a sp?

Please email me directly at jeff@cochand.com with your thoughts.

Thanks,
Jeff
stefan demetz wrote re: Executing a DTS Package From .NET
on 08-08-2004 4:10 PM
case23_69 wrote re: Executing a DTS Package From .NET
on 09-16-2004 9:15 PM
How do we call this from a web page?
David wrote re: Executing a DTS Package From .NET
on 12-10-2004 10:37 AM
I'm just new to VS.NET and also know a bit of DTS.
What is Package2Class? Is this user class? Where can I find this class to test the above code?
Thanks!
David wrote re: Executing a DTS Package From .NET
on 12-10-2004 11:32 AM
It's me again. When I use namespace DTS--> using DTS; , the compiler can not recognize it? What can I do now?
jayson knight wrote re: Executing a DTS Package From .NET
on 12-10-2004 11:34 AM
Take a look at the numbered bullet list in my original post.

1. Project references dialog (right click references, add reference)
2. Find Microsoft DSTPackage Object Library in the exhaustive list of objects on the COM tab
3. Select it, add it...voila. The IDE automagically generates the correct interop assembly and places it in the bin folder, ready for instantiation
Al wrote re: Executing a DTS Package From .NET
on 09-08-2005 12:39 AM
Yeah running packages from ASP.NET is not that straight forward. the code maybe but there are permissions for web server, sql server and sql agent that need need to be setup right.
protected virtual void jaysonBlog { wrote Best of JaysonKnight.com -- Happy New Year!
on 12-30-2005 1:38 AM
One thing that really bugs me about blogs is that it’s still really damn hard to find specific...
Sunjay wrote re: Executing a DTS Package From .NET
on 01-26-2007 9:59 AM

I made a simple asp.net page with a button and when I click that button I would like to execute a DTS package. Does anyone know the code to do this?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Copyright © :: JaysonKnight.com
External Content © :: Respective Authors

Terms of Service/Privacy Policy