1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Runtime.InteropServices;5
using DTS;6

7
namespace ConsoleApplication18


{9
class ExecPkgWithEvents10

{11
12
public Package2Class package;13
[MTAThread]14
static void Main(string[] args)15

{16
ExecPkgWithEvents app = new ExecPkgWithEvents();17
app.Run();18
}19
public void Run()20

{21
try22

{23
package = new Package2Class();24
System.Runtime.InteropServices.ComTypes.IConnectionPointContainer CnnctPtCont = (System.Runtime.InteropServices.ComTypes.IConnectionPointContainer)package;25
System.Runtime.InteropServices.ComTypes.IConnectionPoint CnnctPt;26
PackageEventsSink PES = new PackageEventsSink();27
Guid guid = new Guid("10020605-EB1C-11CF-AE6E-00AA004A34D5"); // UUID of PackageEvents Interface28
CnnctPtCont.FindConnectionPoint(ref guid, out CnnctPt);29
int iCookie;30
CnnctPt.Advise(PES, out iCookie);31
object pVarPersistStgOfHost = null;32

33
package.LoadFromSQLServer("127.0.0.1", null, null, DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, null,34
null, null, "Test", ref pVarPersistStgOfHost);35

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

42
catch (System.Runtime.InteropServices.COMException ex)43

{44
Console.WriteLine("COMException {0}\n{1}\n{2}", ex.ErrorCode, ex.Message, ex.StackTrace);45
}46

47
catch (System.Exception ex)48

{49
Console.WriteLine("Exception\n{0}\n{1}", ex.Message, ex.StackTrace);50
}51
}52

53
}54

55
//This class is responsible for handling DTS Package events. When an event is fired, a message is sent to56
//the console.57
class PackageEventsSink : DTS.PackageEvents58

{59

60
public void OnQueryCancel(string EventSource, ref bool pbCancel)61

{62
Console.WriteLine("OnQueryCancel({0})", EventSource);63
pbCancel = false;64
}65

66
public void OnStart(string EventSource)67

{68
Console.WriteLine("OnStart({0})", EventSource);69
}70

71
public void OnProgress(string EventSource, string ProgressDescription, int PercentComplete, int ProgressCountLow, int ProgressCountHigh)72

{73
Console.WriteLine("OnProgress({0}, {1}, {2}, {3}, {4})", EventSource, ProgressDescription,74
PercentComplete, ProgressCountLow, ProgressCountHigh);75
}76

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

79
IDofInterfaceWithError, ref bool pbCancel)80

{81
Console.WriteLine("OnError({0}, {1}, {2}, {3}, {4}, {5})", EventSource, ErrorCode, Source, Description,82
HelpFile, HelpContext);83
pbCancel = false;84
}85

86
public void OnFinish(string EventSource)87

{88
Console.WriteLine("OnFinish({0})", EventSource);89
Console.Read();90
}91

92

93
}94
}95
