次の方法で共有


SMO イベントの処理

イベント ハンドラーと ServerConnection オブジェクトを使用してサブスクライブできるサーバー イベントの種類があります。

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

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

ConnectionContext イベント処理

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

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

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

イベントを処理するには、イベント ハンドラー クラスを記述する必要があります。 イベント ハンドラー クラスには複数のイベント ハンドラー関数を含めることができます。イベントを処理するにはインストールする必要があります。 イベント ハンドラー関数は、イベントに関する情報を報告するために使用できる ServerEventNotificatificationArgs パラメーターからイベントに関する情報を受け取ります。

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

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

Visual Basic でのイベント ハンドラーの登録とイベント処理のサブスクライブ

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

Visual C でのイベント ハンドラーの登録とイベント処理のサブスクライブ#

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

//Create an event handler subroutine that runs when a table is created.   
private void MyCreateEventHandler(object sender, ServerEventArgs e)   
{   
Console.WriteLine("A table has just been added to the AdventureWorks2012 database.");   
}   
//Create an event handler subroutine that runs when a table is deleted.   
private void MyDropEventHandler(object sender, ServerEventArgs e)   
{   
Console.WriteLine("A table has just been dropped from the AdventureWorks2012 database.");   
}   
public void Main()   
{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Reference the AdventureWorks2012 database.   
Database db;   
db = srv.Databases("AdventureWorks2012");   
//Create a database event set that contains the CreateTable event only.   
DatabaseEventSet databaseCreateEventSet = new DatabaseEventSet();   
databaseCreateEventSet.CreateTable = true;   
//Create a server event handler and set it to the first event handler subroutine.   
ServerEventHandler serverCreateEventHandler;   
serverCreateEventHandler = new ServerEventHandler(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.   
DatabaseEventSet databaseDropEventSet = new DatabaseEventSet();   
databaseDropEventSet.DropTable = true;   
//Create a server event handler and set it to the second event handler subroutine.   
ServerEventHandler serverDropEventHandler;   
serverDropEventHandler = new ServerEventHandler(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.   
Table tb;   
tb = new Table(db, "Test_Table");   
Column mycol1;   
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 occured.   
int x;   
int y;   
for (x = 1; x <= 1000000000; x++) {   
    y = x * 2;   
}   
//Stop event handling.   
db.Events.StopEvents();   
}