サーバー イベントの型には、イベント ハンドラーおよび ServerConnection オブジェクトを使用してサブスクライブできるものがあります。
SQL Server管理オブジェクト (SMO) のインスタンス クラスの多くは、サーバー上の特定のアクションが発生したときにイベントをトリガーできます。
これらのイベントは、イベント ハンドラーを設定し、関連するイベントをサブスクライブすることで、プログラムを使用して処理を行うことができます。 サブスクリプションは SMO クライアント プログラムの終了時にすべて削除されるため、この種類のイベント ハンドリングは一時的なものです。
ConnectionContext イベント ハンドリング
ServerConnection オブジェクトは複数のイベントの種類をサポートしています。 イベント プロパティは、適切なイベント ハンドラーのインスタンスに対して設定されている必要があります。また、イベント ハンドラー オブジェクトは、イベントを処理するプロテクト関数として定義されている必要があります。
イベント サブスクリプション
イベントを処理するには、イベント ハンドラー クラスを作成し、イベント ハンドラー クラスのインスタンスの作成して、イベント ハンドラーを親オブジェクトに割り当てて、イベントをサブスクライブします。
イベントを処理するためには、イベント ハンドラー クラスが作成される必要があります。 イベント ハンドラー クラスは、2 つ以上のイベント ハンドラー関数を含めることができます。また、処理されるイベントに対してインストールされている必要があります。 イベント ハンドラー関数は、イベントに関する情報を報告するために使用できる ServerEventNotificatificationArgs パラメーターからイベントに関する情報を受け取ります。
処理できるデータベース イベントとサーバー イベントの種類は、 DatabaseEventSet クラスと クラスに ServerEventSet一覧表示されます。
例
提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。 詳細については、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();
}