Olayları programlı olarak işleme
The SSIS runtime provides a koleksiyon of events that occur before, during, and after the validation and execution of a paket. Bu olaylar, iki yolla yakalanabilir.Ilk yöntemdir uygulayarak IDTSEvents bir sınıf arabirim ve sınıfı, bir parametre olarak sağlama Execute ve Validate Paket yöntemleri. Ikinci yöntem oluşturarak olur. DtsEventHandler nesneleri, diğer içerebilir SSIS Görevler'de bir olay gerçekleştiğinde yürütülen döngülere gibi nesneleri IDTSEvents oluş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 alınıyor
Geliştiriciler oluşturup paketleri programsal olarak çalıştırın, olay bildirimlerini alabilir, geçerliliği ve yürütmek işlemini kullanarak sırasında IDTSEvents arabirim. Bunu uygulayan bir sınıf oluşturarak yapılır IDTSEvents Bu sınıf, bir parametre olarak sağlayarak arabirim veValidate ve Execute bir paketin yöntemleri. Sınıfın yöntemlerini sonra çalışma-adlandırılır saat olayları oluştuğunda altyapısını.
The DefaultEvents class is a class that already implements the IDTSEvents arabirim; therefore, another alternative to implementing IDTSEvents directly is to derive from DefaultEvents and override the specific events that you want to respond to. Sonra kullanacağınız sınıf için parametre olarak sağladığınız Validate ve Execute yöntemleri Package Olay geri almak için .
Aşağıdaki kod örneği den türetilmiş bir sınıf gösterir. DefaultEventsve geçersiz kılar OnPreExecute(Executable, Boolean%) yöntem. The class is then provided as aparameter to the Validate and Execute methods of the package.
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
DtsEventHandler nesneleri oluşturma
Çalışma-saat güçlü, esnek bir çok olay işleme ve bildirim sistemi aracılığıyla altyapısı sağlar DtsEventHandler nesne. Bu nesneler olay işleyicisi içinde yalnızca olay işleyicisinin ait olduğu bir olay ortaya çıktığında, yürütmek tüm iş tasarlamanıza olanak tanır.The DtsEventHandler object is a kapsayıcı that is executed when the corresponding olay on its parent object fires. Bu mimariye bir kapsayıcı üzerinde oluşan olaylara yanıt olarak gerçekleştirilen yalıtılmış bir iş akışı oluşturmanıza olanak sağlar.Çünkü DtsEventHandler nesneleri zaman uyumlu olduğu belirtilmemiştir, olaya bağlı olay işleyicileri iade kadar yürütmeye devam.
Aşağıdaki kod nasıl oluşturulacağını gösterir bir DtsEventHandler nesne. Kod ekler bir FileSystemTask için Executables() koleksiyon paket ve sonra bir DtsEventHandler için nesne OnError(DtsObject, Int32, String, String, String, Int32, String) Görevin olay. C FileSystemTask eklenen olay işleyicisi, hangi zaman yürütülen OnError(DtsObject, Int32, String, String, String, Int32, String) olay için ilk ortaya çıkar. FileSystemTask. Bu örnek, sınama için C:\Windows\Temp\DemoFile.txt adlı bir dosya olduğunu varsayar.Ilk kez çalıştırdığınız, örnek, dosya başarıyla kopyalar ve olay işleyicisi olarak adlandırılır.Ikinci örnek, çalıştırma saat ilk FileSystemTask dosya kopyalama başarısız (nedeni değeri OverwriteDestinationFile() olan false), olay işleyici olarak adlandırılan ikinci FileSystemTask kaynak dosyası ve paket raporlarını hatası 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();
}
}
}
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
|
See Also