您可以使用事件處理程式和 ServerConnection 物件來訂閱伺服器事件類型。
當伺服器上發生特定動作時,SQL Server 管理物件 (SMO) 中的許多實例類別都可以觸發事件。
設定事件處理程式並訂閱相關聯的事件,即可以程式設計方式處理這些事件。 這種類型的事件處理是暫時性的,因為 SMO 用戶端程式結束時會移除所有訂閱。
ConnectionContext 事件處理
物件 ServerConnection 支援數種事件類型。 事件屬性必須設定為適當事件處理程序的實例,而且事件處理程式對象必須定義為處理事件的受保護函式。
事件訂閱
您可以撰寫事件處理程序類別、建立它的實例、將事件處理程式指派給父對象,然後訂閱事件,以處理事件。
事件處理程式類別必須寫入以處理事件。 事件處理程式類別可以包含多個事件處理程式函式,而且必須安裝才能處理的事件。 事件處理程式函式會從 ServerEventNotificatificationArgs 參數接收事件的相關信息,可用來報告事件的相關信息。
可以處理的資料庫和伺服器事件類型會列在 類別和ServerEventSet類別中DatabaseEventSet。
範例
若要使用提供的任何程式代碼範例,您必須選擇程式設計環境、程式設計範本,以及用來建立應用程式的程式設計語言。 如需詳細資訊,請參閱《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();
}