Olayları programlı olarak işleme
SSISÖnce süresince ve sonra doğrulama meydana gelen olayları topluluğu ve bir paketin runtime sağlar. Bu olayların iki şekilde yakalanabilir. İlk yöntem uygulayarak olduğunu IDTSEventsarabirim bir sınıfta ve sınıf bir parametresi olarak tedarik Executeve Validatepaketi yöntemleri. İkinci yöntem oluşturmaktır DtsEventHandlerda içeren diğer nesnesi, SSISgörevleri ve döngüler, bu bir olay olduğunda yürütülür gibi nesneleri IDTSEventsoluşur. Bu bölümde bu iki yöntem açıklanır ve kullanımları göstermek için kod örnekleri sağlar.
IDTSEvents geri alma
İnşa ve paketleri program aracılığıyla çalıştırmak geliştiriciler, olay bildirimleri alabilir, doğrulama ve kullanılarak yürütme işlemi sırasında IDTSEventsarabirimi. Bunu uygulayan bir sınıf oluşturarak yapılır IDTSEventsarabirimi ve parametre olarak bu sınıf sağlayan Validateve Executeyöntem paket. Olaylar gerçekleştiğinde sınıfının yöntemlerini sonra çalışma zamanı altyapısı tarafından denir.
DefaultEventsSınıftır zaten uygulayan bir sınıfın IDTSEventsarabirimi; Bu nedenle, uygulama başka bir alternatif IDTSEventsdoğrudan elde etmektir DefaultEventsve geçersiz yanıt vermek istediğiniz belirli olay. Sonra sınıf parametre olarak size Validateve Executeyöntemleri Packageolay geriçağırımları alacak.
Aşağıdaki kod örneği göstermektedir dan türeyen sınıf DefaultEventsve geçersiz kılar OnPreExecuteyöntemi. Sınıf sonra sağlanır birparametresi Validateve Executepaketi yöntemleri.
using System;
using Microsoft.SqlServer.Dts.Runtime;
namespace Microsoft.SqlServer.Dts.Samples
{
class Program
{
static void Main(string[] args)
{
Package p = new Package();
MyEventsClass eventsClass = new MyEventsClass();
p.Validate(null, null, eventsClass, null);
p.Execute(null, null, eventsClass, null, null);
Console.Read();
}
}
class MyEventsClass : DefaultEvents
{
public override void OnPreExecute(Executable exec, ref bool fireAgain)
{
// TODO: Add custom code to handle the event.
Console.WriteLine("The PreExecute event of the " +
exec.ToString() + " has been raised.");
}
}
}
using System;
using Microsoft.SqlServer.Dts.Runtime;
namespace Microsoft.SqlServer.Dts.Samples
{
class Program
{
static void Main(string[] args)
{
Package p = new Package();
MyEventsClass eventsClass = new MyEventsClass();
p.Validate(null, null, eventsClass, null);
p.Execute(null, null, eventsClass, null, null);
Console.Read();
}
}
class MyEventsClass : DefaultEvents
{
public override void OnPreExecute(Executable exec, ref bool fireAgain)
{
// TODO: Add custom code to handle the event.
Console.WriteLine("The PreExecute event of the " +
exec.ToString() + " has been raised.");
}
}
}
Imports Microsoft.SqlServer.Dts.Runtime
Module Module1
Sub Main()
Dim p As Package = New Package()
Dim eventsClass As MyEventsClass = New MyEventsClass()
p.Validate(Nothing, Nothing, eventsClass, Nothing)
p.Execute(Nothing, Nothing, eventsClass, Nothing, Nothing)
Console.Read()
End Sub
End Module
Class MyEventsClass
Inherits DefaultEvents
Public Overrides Sub OnPreExecute(ByVal exec As Executable, ByRef fireAgain As Boolean)
' TODO: Add custom code to handle the event.
Console.WriteLine("The PreExecute event of the " & _
exec.ToString() & " has been raised.")
End Sub
End Class
Imports Microsoft.SqlServer.Dts.Runtime
Module Module1
Sub Main()
Dim p As Package = New Package()
Dim eventsClass As MyEventsClass = New MyEventsClass()
p.Validate(Nothing, Nothing, eventsClass, Nothing)
p.Execute(Nothing, Nothing, eventsClass, Nothing, Nothing)
Console.Read()
End Sub
End Module
Class MyEventsClass
Inherits DefaultEvents
Public Overrides Sub OnPreExecute(ByVal exec As Executable, ByRef fireAgain As Boolean)
' TODO: Add custom code to handle the event.
Console.WriteLine("The PreExecute event of the " & _
exec.ToString() & " has been raised.")
End Sub
End Class
DtsEventHandler nesneleri oluşturma
Çalışma zamanı altyapısı sağlam, son derece esnek olay işleme ve bildirim sistemi aracılığıyla sağlar DtsEventHandlernesnesini. Bu nesneler, hangi yürütme sadece olay işleyicisi ait olay gerçekleştiğinde tüm iş akışlarını olay işleyicisi içinde tasarlamanıza olanak tanır. DtsEventHandlerÜst nesne üzerinde ilgili olay yangınlar durumda çalıştırılan bir kapsayıcı nesnesidir. Bu mimari bir kapsayıcı meydana gelen olaylara yanıt olarak yürütülen izole iş akışları oluşturmanızı sağlar. Çünkü DtsEventHandlernesnelerini eşzamanlı, yürütülmesine değil devam kadar olaya bağlı olan olay işleyicileri geri döndü.
Aşağıdaki kodu nasıl oluşturulacağını gösterir bir DtsEventHandlernesnesini. Kod ekler bir FileSystemTaskiçin Executableskoleksiyonu paketin ve oluşturan bir DtsEventHandlernesne için OnErrorolay görev. A FileSystemTaskolan olay işleyicisine eklenen zaman idam OnErrorolay ilk ortaya FileSystemTask. Bu örnek, test etmek için C:\Windows\Temp\DemoFile.txt adlı bir dosya olduğunu varsayar. Eğer örnek çalışan ilk kez başarıyla dosya kopyalar ve olay işleyicisi değil denir. Sen koşmak örnek, ikinci kez ilk FileSystemTaskdosyayı kopyalamak başarısız (çünkü değeri OverwriteDestinationFileolan false), Olay İşleyici çağrıldığında, ikinci FileSystemTaskkaynak dosya ve paketi raporları hata oluştu hata nedeniyle siler.
Örnek
using System;
using System.IO;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.FileSystemTask;
namespace Microsoft.SqlServer.Dts.Samples
{
class Program
{
static void Main(string[] args)
{
string f = "DemoFile.txt";
Executable e;
TaskHost th;
FileSystemTask fst;
Package p = new Package();
p.Variables.Add("sourceFile", true, String.Empty,
@"C:\Windows\Temp\" + f);
p.Variables.Add("destinationFile", true, String.Empty,
Path.Combine(Directory.GetCurrentDirectory(), f));
// Create a first File System task and add it to the package.
// This task tries to copy a file from one folder to another.
e = p.Executables.Add("STOCK:FileSystemTask");
th = e as TaskHost;
th.Name = "FileSystemTask1";
fst = th.InnerObject as FileSystemTask;
fst.Operation = DTSFileSystemOperation.CopyFile;
fst.OverwriteDestinationFile = false;
fst.Source = "sourceFile";
fst.IsSourcePathVariable = true;
fst.Destination = "destinationFile";
fst.IsDestinationPathVariable = true;
// Add an event handler for the FileSystemTask's OnError event.
DtsEventHandler ehOnError = (DtsEventHandler)th.EventHandlers.Add("OnError");
// Create a second File System task and add it to the event handler.
// This task deletes the source file if the event handler is called.
e = ehOnError.Executables.Add("STOCK:FileSystemTask");
th = e as TaskHost;
th.Name = "FileSystemTask2";
fst = th.InnerObject as FileSystemTask;
fst.Operation = DTSFileSystemOperation.DeleteFile;
fst.Source = "sourceFile";
fst.IsSourcePathVariable = true;
DTSExecResult vr = p.Validate(null, null, null, null);
Console.WriteLine("ValidationResult = " + vr.ToString());
if (vr == DTSExecResult.Success)
{
DTSExecResult er = p.Execute(null, null, null, null, null);
Console.WriteLine("ExecutionResult = " + er.ToString());
if (er == DTSExecResult.Failure)
if (!File.Exists(@"C:\Windows\Temp\" + f))
Console.WriteLine("Source file has been deleted by the event handler.");
}
Console.Read();
}
}
}
using System;
using System.IO;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.FileSystemTask;
namespace Microsoft.SqlServer.Dts.Samples
{
class Program
{
static void Main(string[] args)
{
string f = "DemoFile.txt";
Executable e;
TaskHost th;
FileSystemTask fst;
Package p = new Package();
p.Variables.Add("sourceFile", true, String.Empty,
@"C:\Windows\Temp\" + f);
p.Variables.Add("destinationFile", true, String.Empty,
Path.Combine(Directory.GetCurrentDirectory(), f));
// Create a first File System task and add it to the package.
// This task tries to copy a file from one folder to another.
e = p.Executables.Add("STOCK:FileSystemTask");
th = e as TaskHost;
th.Name = "FileSystemTask1";
fst = th.InnerObject as FileSystemTask;
fst.Operation = DTSFileSystemOperation.CopyFile;
fst.OverwriteDestinationFile = false;
fst.Source = "sourceFile";
fst.IsSourcePathVariable = true;
fst.Destination = "destinationFile";
fst.IsDestinationPathVariable = true;
// Add an event handler for the FileSystemTask's OnError event.
DtsEventHandler ehOnError = (DtsEventHandler)th.EventHandlers.Add("OnError");
// Create a second File System task and add it to the event handler.
// This task deletes the source file if the event handler is called.
e = ehOnError.Executables.Add("STOCK:FileSystemTask");
th = e as TaskHost;
th.Name = "FileSystemTask2";
fst = th.InnerObject as FileSystemTask;
fst.Operation = DTSFileSystemOperation.DeleteFile;
fst.Source = "sourceFile";
fst.IsSourcePathVariable = true;
DTSExecResult vr = p.Validate(null, null, null, null);
Console.WriteLine("ValidationResult = " + vr.ToString());
if (vr == DTSExecResult.Success)
{
DTSExecResult er = p.Execute(null, null, null, null, null);
Console.WriteLine("ExecutionResult = " + er.ToString());
if (er == DTSExecResult.Failure)
if (!File.Exists(@"C:\Windows\Temp\" + f))
Console.WriteLine("Source file has been deleted by the event handler.");
}
Console.Read();
}
}
}
Imports System.IO
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.SqlServer.Dts.Tasks.FileSystemTask
Module Module1
Sub Main()
Dim f As String = "DemoFile.txt"
Dim e As Executable
Dim th As TaskHost
Dim fst As FileSystemTask
Dim p As Package = New Package()
p.Variables.Add("sourceFile", True, String.Empty, _
"C:\Windows\Temp\" & f)
p.Variables.Add("destinationFile", True, String.Empty, _
Path.Combine(Directory.GetCurrentDirectory(), f))
' Create a first File System task and add it to the package.
' This task tries to copy a file from one folder to another.
e = p.Executables.Add("STOCK:FileSystemTask")
th = CType(e, TaskHost)
th.Name = "FileSystemTask1"
fst = CType(th.InnerObject, FileSystemTask)
fst.Operation = DTSFileSystemOperation.CopyFile
fst.OverwriteDestinationFile = False
fst.Source = "sourceFile"
fst.IsSourcePathVariable = True
fst.Destination = "destinationFile"
fst.IsDestinationPathVariable = True
' Add an event handler for the FileSystemTask's OnError event.
Dim ehOnError As DtsEventHandler = CType(th.EventHandlers.Add("OnError"), DtsEventHandler)
' Create a second File System task and add it to the event handler.
' This task deletes the source file if the event handler is called.
e = ehOnError.Executables.Add("STOCK:FileSystemTask")
th = CType(e, TaskHost)
th.Name = "FileSystemTask1"
fst = CType(th.InnerObject, FileSystemTask)
fst.Operation = DTSFileSystemOperation.DeleteFile
fst.Source = "sourceFile"
fst.IsSourcePathVariable = True
Dim vr As DTSExecResult = p.Validate(Nothing, Nothing, Nothing, Nothing)
Console.WriteLine("ValidationResult = " + vr.ToString())
If vr = DTSExecResult.Success Then
Dim er As DTSExecResult = p.Execute(Nothing, Nothing, Nothing, Nothing, Nothing)
Console.WriteLine("ExecutionResult = " + er.ToString())
If er = DTSExecResult.Failure Then
If Not File.Exists("C:\Windows\Temp\" + f) Then
Console.WriteLine("Source file has been deleted by the event handler.")
End If
End If
End If
Console.Read()
End Sub
End Module
Imports System.IO
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.SqlServer.Dts.Tasks.FileSystemTask
Module Module1
Sub Main()
Dim f As String = "DemoFile.txt"
Dim e As Executable
Dim th As TaskHost
Dim fst As FileSystemTask
Dim p As Package = New Package()
p.Variables.Add("sourceFile", True, String.Empty, _
"C:\Windows\Temp\" & f)
p.Variables.Add("destinationFile", True, String.Empty, _
Path.Combine(Directory.GetCurrentDirectory(), f))
' Create a first File System task and add it to the package.
' This task tries to copy a file from one folder to another.
e = p.Executables.Add("STOCK:FileSystemTask")
th = CType(e, TaskHost)
th.Name = "FileSystemTask1"
fst = CType(th.InnerObject, FileSystemTask)
fst.Operation = DTSFileSystemOperation.CopyFile
fst.OverwriteDestinationFile = False
fst.Source = "sourceFile"
fst.IsSourcePathVariable = True
fst.Destination = "destinationFile"
fst.IsDestinationPathVariable = True
' Add an event handler for the FileSystemTask's OnError event.
Dim ehOnError As DtsEventHandler = CType(th.EventHandlers.Add("OnError"), DtsEventHandler)
' Create a second File System task and add it to the event handler.
' This task deletes the source file if the event handler is called.
e = ehOnError.Executables.Add("STOCK:FileSystemTask")
th = CType(e, TaskHost)
th.Name = "FileSystemTask1"
fst = CType(th.InnerObject, FileSystemTask)
fst.Operation = DTSFileSystemOperation.DeleteFile
fst.Source = "sourceFile"
fst.IsSourcePathVariable = True
Dim vr As DTSExecResult = p.Validate(Nothing, Nothing, Nothing, Nothing)
Console.WriteLine("ValidationResult = " + vr.ToString())
If vr = DTSExecResult.Success Then
Dim er As DTSExecResult = p.Execute(Nothing, Nothing, Nothing, Nothing, Nothing)
Console.WriteLine("ExecutionResult = " + er.ToString())
If er = DTSExecResult.Failure Then
If Not File.Exists("C:\Windows\Temp\" + f) Then
Console.WriteLine("Source file has been deleted by the event handler.")
End If
End If
End If
Console.Read()
End Sub
End Module
|