SMO イベントの処理

適用対象:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse Analytics

サーバー イベントの型には、イベント ハンドラーおよび ServerConnection オブジェクトを使用してサブスクライブできるものがあります。

SQL Server 管理オブジェクト (SMO) のインスタンス クラスの多くは、サーバー上の特定のアクションが発生したときにイベントをトリガーできます。

これらのイベントは、イベント ハンドラーを設定し、関連するイベントをサブスクライブすることで、プログラムを使用して処理を行うことができます。 サブスクリプションは SMO クライアント プログラムの終了時にすべて削除されるため、この種類のイベント ハンドリングは一時的なものです。

ConnectionContext イベント処理

ServerConnection オブジェクトは複数のイベントの種類をサポートしています。 イベント プロパティは、適切なイベント ハンドラーのインスタンスに対して設定されている必要があります。また、イベント ハンドラー オブジェクトは、イベントを処理するプロテクト関数として定義されている必要があります。

イベント サブスクリプション

イベントを処理するには、イベント ハンドラー クラスを作成し、イベント ハンドラー クラスのインスタンスの作成して、イベント ハンドラーを親オブジェクトに割り当てて、イベントをサブスクライブします。

イベントを処理するためには、イベント ハンドラー クラスが作成される必要があります。 イベント ハンドラー クラスは、2 つ以上のイベント ハンドラー関数を含めることができます。また、処理されるイベントに対してインストールされている必要があります。 イベント ハンドラー関数は、イベントに関する情報を 報告するために使用できる ServerEventNotificatificationArgs パラメーターからイベントに関する情報を受け取ります。

処理できるデータベース イベントとサーバー イベントの種類は、クラスとクラスにDatabaseEventSetServerEventSet一覧表示されます。

提供されているコード例を使用するには、プログラミング環境、プログラミング テンプレート、およびアプリケーションを作成するプログラミング言語を選択する必要があります。 詳細については、「Visual Studio .NET で Visual C# SMO プロジェクトを作成する方法」を参照してください

.NET でイベント ハンドラーを登録し、イベント処理をサブスクライブする

次のコード サンプルは、.NET でイベント ハンドラーを設定する方法と、データベース イベントをサブスクライブする方法を示しています。

Visual Basic

このコード例では、イベント ハンドラーを設定する方法およびデータベース イベントをサブスクライブする方法を示します。

'Create an event handler subroutine that runs when a table is created.
Private Sub MyCreateEventHandler(ByVal sender As Object, ByVal e As ServerEventArgs)
    Console.WriteLine("A table has just been added to the AdventureWorks2022 database.")
End Sub

'Create an event handler subroutine that runs when a table is deleted.
Private Sub MyDropEventHandler(ByVal sender As Object, ByVal e As ServerEventArgs)
    Console.WriteLine("A table has just been dropped from the AdventureWorks2022 database.")
End Sub

Sub Main()
    'Connect to the local, default instance of SQL Server.

    Dim srv As Server
    srv = New Server

    'Reference the AdventureWorks2022 database.
    Dim db As Database
    db = srv.Databases("AdventureWorks2022")

    'Create a database event set that contains the CreateTable event only.
    Dim databaseCreateEventSet As New DatabaseEventSet
    databaseCreateEventSet.CreateTable = True

    'Create a server event handler and set it to the first event handler subroutine.
    Dim serverCreateEventHandler As ServerEventHandler
    serverCreateEventHandler = New ServerEventHandler(AddressOf MyCreateEventHandler)

    'Subscribe to the first server event handler when a CreateTable event occurs.
    db.Events.SubscribeToEvents(databaseCreateEventSet, serverCreateEventHandler)

    'Create a database event set that contains the DropTable event only.
    Dim databaseDropEventSet As New DatabaseEventSet
    databaseDropEventSet.DropTable = True

    'Create a server event handler and set it to the second event handler subroutine.
    Dim serverDropEventHandler As ServerEventHandler
    serverDropEventHandler = New ServerEventHandler(AddressOf MyDropEventHandler)

    'Subscribe to the second server event handler when a DropTable event occurs.
    db.Events.SubscribeToEvents(databaseDropEventSet, serverDropEventHandler)

    'Start event handling.
    db.Events.StartEvents()

    'Create a table on the database.
    Dim tb As Table
    tb = New Table(db, "Test_Table")

    Dim mycol1 As Column
    mycol1 = New Column(tb, "Name", DataType.NChar(50))
    mycol1.Collation = "Latin1_General_CI_AS"
    mycol1.Nullable = True

    tb.Columns.Add(mycol1)
    tb.Create()

    'Remove the table.
    tb.Drop()

    'Wait until the events have occurred.
    Dim x As Integer
    Dim y As Integer
    For x = 1 To 1000000000
        y = x*2
    Next

    'Stop event handling.
    db.Events.StopEvents()
End Sub