處理 SMO 事件
有些伺服器事件類型可以藉由事件處理常式和 ServerConnection 物件進行訂閱。
SQL Server 管理物件 (SMO) 中的許多執行個體類別,都會在伺服器上發生特定動作時觸發事件。
這些事件可以藉由設定事件處理常式及訂閱相關事件,以程式設計的方式來處理。 這種事件處理類型是暫時性的,因為當 SMO 用戶端程式結束時會將訂閱全數移除。
ConnectionContext 事件處理
ServerConnection 物件支援數種事件類型。 事件屬性必須設定為具有適當事件處理常式的執行個體,而事件處理常式物件則必須定義為可以處理事件的受保護函數。
事件訂閱
您可藉由下列步驟來處理事件:撰寫事件處理常式類別、建立其執行個體、將事件處理常式指派給父物件,然後再訂閱事件。
您必須撰寫事件處理常式類別,才能處理事件。 事件處理常式類別可以包含一個以上的事件處理常式函數,而且必須加以安裝,才能處理事件。 事件處理常式函數會從可用於報告事件相關資訊的 ServerEventNotificatificationArgs 參數接收事件的相關資訊。
可以處理的資料庫和伺服器事件類型,會列在 DatabaseEventSet 類別和 ServerEventSet 類別中。
範例
如果要使用所提供的任何程式碼範例,您必須選擇建立應用程式用的程式設計環境、程式設計範本,及程式設計語言。如需詳細資訊,請參閱《SQL Server 線上叢書》中的<如何:在 Visual Studio .NET 中建立 Visual Basic SMO 專案>或<如何:在 Visual Studio .NET 中建立 Visual C# SMO 專案>。
在 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 AdventureWorks2012 2008 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 AdventureWorks2012 2008 database.")
End Sub
Sub Main()
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2012 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'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 occured.
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
在 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();
}