C# 操作DTS

in the C# class file for your project:
using System;
using System.Runtime.InteropServices;
using DTS;

namespace DtsInterop
{
	//This class loads and executes the DTS package.
	class ExecPkgWithEvents

	{
		/*	Prior to running this code, create a DTS package and save it to SQL Server. Then set a reference to
		//	the DTSPackage Object Library version 2.0 COM object.
		*/

		public Package2Class package;

		[MTAThread]
		static void Main(string[] args)
		{
			ExecPkgWithEvents app = new ExecPkgWithEvents();
			app.Run();
		}

		public void Run()
		{
			try
			{
				package = new Package2Class();
				UCOMIConnectionPointContainer CnnctPtCont = (UCOMIConnectionPointContainer) package;
				UCOMIConnectionPoint CnnctPt;
				PackageEventsSink PES = new PackageEventsSink ();
				Guid guid = new Guid("10020605-EB1C-11CF-AE6E-00AA004A34D5");  // UUID of PackageEvents Interface
				CnnctPtCont.FindConnectionPoint(ref guid, out CnnctPt);
				int iCookie;
				CnnctPt.Advise(PES, out iCookie);
				object pVarPersistStgOfHost = null;

				package.LoadFromSQLServer("PNXDEVBOX", null, null, DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, null,
					null, null, "Test", ref pVarPersistStgOfHost);

				package.Execute();
				package.UnInitialize();
				package = null;
				CnnctPt.Unadvise(iCookie); //a connection that is created by IConnectionPoint.Advise must be closed by calling IConnectionPoint.Unadvise to avoid a memory leak
			}

			catch(System.Runtime.InteropServices.COMException ex)
			{
				Console.WriteLine("COMException {0}/n{1}/n{2}", ex.ErrorCode, ex.Message, ex.StackTrace);
			}

			catch(System.Exception ex)
			{
				Console.WriteLine("Exception/n{0}/n{1}", ex.Message, ex.StackTrace);
			}
		}

	}

	//This class is responsible for handling DTS Package events. When an event is fired, a message is sent to
	//the console.
	class PackageEventsSink : DTS.PackageEvents
	{

		public void OnQueryCancel(string EventSource, ref bool pbCancel)
		{
			Console.WriteLine("OnQueryCancel({0})", EventSource);
			pbCancel = false;
		}

		public void OnStart(string EventSource)
		{
			Console.WriteLine("OnStart({0})", EventSource);
		}

		public void OnProgress(string EventSource, string ProgressDescription, int PercentComplete, int ProgressCountLow, int ProgressCountHigh)
		{
			Console.WriteLine("OnProgress({0}, {1}, {2}, {3}, {4})", EventSource, ProgressDescription,
				PercentComplete, ProgressCountLow, ProgressCountHigh);
		}

		public void OnError(string EventSource, int ErrorCode, string Source, string Description, string HelpFile, int HelpContext, string

			IDofInterfaceWithError, ref bool pbCancel)
		{
			Console.WriteLine("OnError({0}, {1}, {2}, {3}, {4}, {5})", EventSource, ErrorCode, Source, Description,
				HelpFile, HelpContext);
			pbCancel = false;
		}

		public void OnFinish(string EventSource)
		{
			Console.WriteLine("OnFinish({0})", EventSource);
		}

	}

转载于:https://www.cnblogs.com/dhz123/archive/2004/12/14/2194365.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值