free geoip Executing a DTS Package From .NET - protected virtual void jaysonBlog
in

jaysonKnight.com

Welcome to my corner of the internet

protected virtual void jaysonBlog

A conduit to the voices in my head

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.

Share this post: Submit Executing+a+DTS+Package+From+.NET to Technorati | Submit Executing+a+DTS+Package+From+.NET to del.icio.us | Submit Executing+a+DTS+Package+From+.NET to digg.com | Submit Executing+a+DTS+Package+From+.NET to reddit.com | Submit Executing+a+DTS+Package+From+.NET to DotNetKicks | Add Executing+a+DTS+Package+From+.NET to Live Bookmarks

Comments

 

Jeff Cochand said:

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
July 22, 2004 5:24 PM
 

stefan demetz said:

August 8, 2004 4:10 PM
 

case23_69 said:

How do we call this from a web page?
September 16, 2004 9:15 PM
 

David said:

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!
December 10, 2004 10:37 AM
 

David said:

It's me again. When I use namespace DTS--> using DTS; , the compiler can not recognize it? What can I do now?
December 10, 2004 11:32 AM
 

jayson knight said:

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
December 10, 2004 11:34 AM
 

Al said:

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.
September 8, 2005 12:39 AM
 

protected virtual void jaysonBlog { said:

One thing that really bugs me about blogs is that it’s still really damn hard to find specific...
December 30, 2005 1:38 AM
 

Sunjay said:

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?

January 26, 2007 9:59 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About Jayson Knight

Jayson Knight was clueless to the computer programming world until he took a C++ class in college. The rest is proverbial history. He has been building applications targeting the .Net framework for 7 years, focusing mainly on internet technologies and database driven web application development.

Most recently he left the world of Corporate IT to finish up his degree in Chemistry, with an eye on Medical School and an Anesthesiology residency program. Read this post for more information.

He is also a Community Server MVP: Community Server is the software that runs this site, plus many others on the web. For more information, check out http://csmvps.com.

When he finds time to pry himself away from his computer and university studies, he can be found on the mountain bike trails when it's warm, and on the ski slopes when it's cold.

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

Terms of Service/Privacy Policy