共用方式為


Database.Events 屬性

Gets the events associated with the database.

命名空間:  Microsoft.SqlServer.Management.Smo
組件:  Microsoft.SqlServer.Smo (在 Microsoft.SqlServer.Smo.dll 中)

語法

'宣告
Public ReadOnly Property Events As DatabaseEvents 
    Get
'用途
Dim instance As Database 
Dim value As DatabaseEvents 

value = instance.Events
public DatabaseEvents Events { get; }
public:
property DatabaseEvents^ Events {
    DatabaseEvents^ get ();
}
member Events : DatabaseEvents
function get Events () : DatabaseEvents

屬性值

型別:Microsoft.SqlServer.Management.Smo.DatabaseEvents
A DatabaseEvents object that represents the database events.

備註

The Events property refers to server events or Windows Management Instrumentation (WMI) events that are associated with the database.

範例

VB

'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

PowerShell

Function MyCreateEventHandler($sender, $e)
{
   Write-Host "A table has just been added to the AdventureWorks2012 database."
}
Function MyDropEventHandler($sender, $e)
{
   Write-Host "A table has just been dropped from the AdventureWorks2012 database."
}
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("AdventureWorks2012")

$databaseCreateEventSet = new-object Microsoft.SqlServer.Management.Smo.DatabaseEventSet
$databaseCreateEventSet.CreateTable = $TRUE
$serverCreateEventHandler = new-object Microsoft.SqlServer.Management.Smo.ServerEventHandler(AddressOf MyCreateEventHandler)
$db.Events.SubscribeToEvents(databaseCreateEventSet, serverCreateEventHandler)
$databaseDropEventSet = new-object Microsoft.SqlServer.Management.Smo.DatabaseEventSet
$databaseDropEventSet.DropTable = $TRUE

$serverDropEventHandler = new-object Microsoft.SqlServer.Management.Smo.ServerEventHandler(AddressOf MyDropEventHandler)
$db.Events.SubscribeToEvents(databaseDropEventSet, serverDropEventHandler)
$db.Events.StartEvents()

$tb = new-object Microsoft.SqlServer.Management.Smo.Table($db, "Test Table")
$mycol1 = new-object Microsoft.SqlServer.Management.Smo.Column($tb, "Name", [Microsoft.SqlServer.Management.Smo.DataType]::NChar(50))
$mycol1.Collation = "Latin1_General_CI_AS"
$mycol1.Nullable = $FALSE
$tb.Columns.Add($mycol1)
$tb.Create()

$tb.Drop()
$y = 0
For ($x=1; $x -le 1000000000; $x++)
{
   $y = $x*2
}
$db.Events.StopEvents()

請參閱

參考

Database 類別

Microsoft.SqlServer.Management.Smo 命名空間

其他資源

使用資料庫物件

Handling Events (SMO)

建立、改變和移除資料庫

CREATE DATABASE (Transact-SQL)