TechNet
Export (0) Print
Expand All
Collapse the table of content
Expand the table of content
Expand Minimize

Database.Events Property

 

Gets the events associated with the database.

Namespace:   Microsoft.SqlServer.Management.Smo
Assembly:  Microsoft.SqlServer.Smo (in Microsoft.SqlServer.Smo.dll)

public DatabaseEvents Events { get; }

Property Value

Type: 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

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()

Return to top
Show:
© 2016 Microsoft