Olayları programlı olarak işleme

The SSIS runtime provides a collection of events that occur before, during, and after the validation and execution of a package.Bu olaylar, iki yolla yakalanabilir.İlk yöntem uygulayarak olan IDTSEvents bir sınıfta arabirim ve parametre olarak sınıf Johannes Execute ve Validate yöntemleri, paket.İkinci yöntem oluşturmaktır DtsEventHandler , içerebilir diğer nesneleri SSIS görevleri ve halkalar, zaman içinde bir olay yürütülen gibi nesneleri IDTSEvents oluşur.Bu bölümde bu iki yöntem açıklanır ve bunların kullanımını gösteren kod örnekleri sağlar.

IDTSEvents geri alma

Oluşturmak ve paketleri program aracılığıyla çalıştırmak geliştiriciler doğrulama ve işlemi kullanılarak yürütmek sırasında olay bildirimlerini almak IDTSEvents arabirim.Bunu uygulayan bir sınıf oluşturarak yapılır IDTSEvents arabirim ve bu sınıfın bir parametre olarak sağlayan Validate ve Execute yöntemleri, paket.Sınıfının yöntemleri ardından run - tarafından verilirsaat altyapısı olaylar oluştuğunda.

The DefaultEvents class is a class that already implements the IDTSEvents interface; therefore, another alternative to implementing IDTSEvents directly is to derive from DefaultEvents and override the specific events that you want to respond to.Sonra Sınıfınızın bir parametre olarak verdiğiniz Validate ve Execute yöntemleri Package almak için olay geri aramalar.

Aşağıdaki kod örneği türetildiği bir sınıfı gösterir DefaultEvents, geçersiz kılar ve OnPreExecute 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

Run -saat altyapısı sağlayan güçlü, son derece esnek olay işleme ve bildirim sistemi aracılığıyla DtsEventHandler nesne.Bu nesnelerin iş yalnızca olay işleyicisi ait olduğu olay gerçekleştiğinde, yürütmek bütün akışları da olay iþleyicisi içinde tasarlamanıza olanak tanır.The DtsEventHandler object is a container that is executed when the corresponding event on its parent object fires.Bu mimari bir kapsayıcı üzerinde oluşan olaylara yanıt olarak yürütülen yalıtılmış iş akışları oluşturmanızı sağlar.Çünkü DtsEventHandler nesneleri zaman uyumlu, yürütme olaya bağlı olay işleyicileri döndüğünüzü kadar geri çağrılmıyor.

Aşağıdaki kodu nasıl oluşturulacağını gösterir bir DtsEventHandler nesne.Kod ekleyen bir FileSystemTask için Executables koleksiyon paket ve sonra bir DtsEventHandler için nesne OnError olay görev.A FileSystemTask eklenen olay işleyicisi, hangi ne zaman yürütülecek olan OnError olay ortaya ilk kez FileSystemTask.Bu örnek, test etmek için C:\Windows\Temp\DemoFile.txt adlı bir dosya olduğunu varsayar.İlk saat örnek, çalıştırma dosyası başarıyla kopyalar ve olay işleyicisi olarak adlandırılır.İkinci saat örnek çalıştırın ilk FileSystemTask dosyayı kopyalamak başarısız (çünkü değeri OverwriteDestinationFile olan false), olay işleyicisi olarak adlandırılan ikinci FileSystemTask kaynak dosyası ve paket 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();
    }
  }
}
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
Integration Services simgesi (küçük)Integration Services ile güncel kalın

En son karşıdan yüklemeler, makaleler, örnekler ve seçilen topluluk çözümleri yanı sıra Microsoft videolar için ziyaret Integration Services sayfa msdn veya TechNet:

Bu güncelleştirmelerle ilgili otomatik bildirim almak için, sayfadaki RSS akışlarına abone olun.