Behandeln von SMO-EreignissenHandling SMO Events

DIESES THEMA GILT FÜR: jaSQL Server jaAzure SQL-DatenbankjaAzure SQL Data Warehouse neinParallel Data Warehouse THIS TOPIC APPLIES TO: yesSQL ServeryesAzure SQL DatabaseyesAzure SQL Data Warehouse noParallel Data Warehouse

Es gibt Serverereignistypen, die über einen Ereignishandler und das ServerConnection-Objekt abonniert werden können.There are server event types that can be subscribed to by using an event handler and the ServerConnection object.

Viele der Instanzklassen in SQL ServerSQL Server Management Objects (SMO) können Ereignisse auslösen, wenn bestimmte Aktionen auf dem Server auftreten.Many of the instance classes in SQL ServerSQL Server Management Objects (SMO) can trigger events when certain actions on the server occur.

Diese Ereignisse können programmgesteuert behandelt werden, indem ein Ereignishandler eingerichtet und ein Abonnement für die zugehörigen Ereignisse vorgenommen wird.These events can be handled programmatically by setting up an event handler and subscribing to the associated events. Diese Art der Ereignisbehandlung ist vorübergehend, da alle Abonnements gelöscht werden, sobald das SMO-Clientprogramm beendet wird.This type of event handling is transient because all the subscriptions are removed when the SMO client program exits.

ConnectionContext-EreignisbehandlungConnectionContext Event Handling

Das ServerConnection-Objekt unterstützt mehrere Ereignistypen.The ServerConnection object supports several event types. Die Ereigniseigenschaft muss auf die Instanz eines geeigneten Ereignishandlers gesetzt sein, und das Ereignishandlerobjekt muss als geschützte Funktion definiert sein, die das Ereignis behandelt.The event property must be set to an instance of an appropriate event handler, and the event handler object must be defined as a protected function that handles the event.

EreignisabonnementEvent Subscription

Sie behandeln Ereignisse, indem Sie eine Ereignishandlerklasse schreiben, eine Instanz dieser Klasse erstellen, den Ereignishandler dem übergeordneten Objekt zuordnen und dann das Ereignis abonnieren.You handle events by writing an event handler class, creating an instance of it, assigning the event handler to the parent object, and then subscribing to the event.

Eine Ereignishandlerklasse muss geschrieben werden, um Ereignisse zu behandeln.An event handler class must be written to handle events. Die Ereignishandlerklasse kann mehr als eine Ereignishandlerfunktion enthalten und muss für die zu behandelnden Ereignisse installiert sein.The event handler class can contain more than one event handler function, and must be installed for the events to be handled. Die Ereignishandlerfunktionen erhalten Informationen über das Ereignis aus der ServerEventNotificatificationArgs Parameter, der verwendet werden kann, um Informationen über das Ereignis zu melden.The event handler functions receive information about the event from the ServerEventNotificatificationArgs parameter that can be used to report information about the event.

Die Typen der Datenbank- und Serverereignisse, die behandelt werden können werden angezeigt, der DatabaseEventSet Klasse und die ServerEventSetKlasse.The types of database and server events that can be handled are listed in the DatabaseEventSet class and the ServerEventSetclass.

BeispielExample

Zum Verwenden eines angegebenen Codebeispiels müssen Sie die Programmierumgebung, Programmiervorlage und die zu verwendende Programmiersprache auswählen, um Ihre Anwendung zu erstellen.To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. Weitere Informationen finden Sie unter Erstellen eines Visual C # SMO-Projekts in Visual Studio .NET.For more information, see Create a Visual C# SMO Project in Visual Studio .NET.

Registrieren von Ereignishandlern und Abonnieren der Ereignisbehandlung in Visual BasicRegistering Event Handlers and Subscribing to Event Handling in Visual Basic

Dieses Codebeispiel zeigt, wie der Ereignishandler eingerichtet wird und wie die Datenbankereignisse abonniert werden.This code example shows how to set up the event handler, and how to subscribe to the database events.

'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

Registrieren von Ereignishandlern und Abonnieren der Ereignisbehandlung in Visual C#Registering Event Handlers and Subscribing to Event Handling in Visual C#

Dieses Codebeispiel zeigt, wie der Ereignishandler eingerichtet wird und wie die Datenbankereignisse abonniert werden.This code example shows how to set up the event handler, and how to subscribe to the database events.

//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();   
}