Właściwość Server.Events
Pobiera zdarzeń serwera, które są skojarzone z wystąpienie SQL Server.
Przestrzeń nazw: Microsoft.SqlServer.Management.Smo
Zestaw: Microsoft.SqlServer.Smo (w Microsoft.SqlServer.Smo.dll)
Składnia
'Deklaracja
Public ReadOnly Property Events As ServerEvents
Get
'Użycie
Dim instance As Server
Dim value As ServerEvents
value = instance.Events
public ServerEvents Events { get; }
public:
property ServerEvents^ Events {
ServerEvents^ get ();
}
member Events : ServerEvents
function get Events () : ServerEvents
Wartość właściwości
Typ: Microsoft.SqlServer.Management.Smo.ServerEvents
A ServerEvents obiekt, który reprezentuje zdarzenia serwera.
Uwagi
Właściwość zdarzenia wskazuje na ServerEvents obiektu steruje subskrypcja zdarzeń serwera aplikacji do uchwytu.Użyj ServerEventSet obiekt, aby utworzyć zmienna, która zawiera określony zestaw server zdarzenies, takich jak CreateDatabase zdarzenie.Serwer może subskrybować zdarzenia wywołując jedną z SubscribeToEvents overloads.StartEvents Następnie metoda uruchamia serwer, który odbiera zdarzenia.
Przykłady
'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 AdventureWorks2008R2 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 AdventureWorks2008R2 database.")
End Sub
Sub Main()
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'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
Zobacz także