Tuesday, March 23, 2010

Execute SQL Server DTS Package using your .NET applications / code


Use this code to execute DTS package using your .NET application/code:
(Just change the variables in red below to match your SQL Server environment)



using System;
using System.Runtime.InteropServices;
using DTS;

namespace DotNetArchitect.DTSPackage
{
///
/// Code to execute DTS package using your .NET application/code:
/// (Just change the variables in red below to match your SQL Server environment)
///
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
     SERVER_USER_NAME// server username
     SERVER_PASSWORD// 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
   }
  }
 }
}

No comments: