MonitorEventHandler Delegate
Represents the method that will handle the
TraceEvent event of the
DbMonitor class.
The following sample shows how to enable and disable monitoring of database activity in your application.
static void OnEvent(object sender, MonitorEventArgs e)
{
if (e.TracePoint==MonitorTracePoint.BeforeEvent)
{
Console.WriteLine("Description: " + e.Description);
Console.WriteLine(" Extra info: " + e.ExtraInfo);
}
}
[MTAThread]
static void Main(string[] args)
{
SqlConnection myConn = new SqlConnection(
"User Id=sa;Server=localhost;Initial Catalog=Test;");
SqlMonitor myMonitor = new SqlMonitor();
myMonitor.TraceEvent += new MonitorEventHandler(OnEvent);
myMonitor.IsActive = true;
SqlCommand myCommand = new SqlCommand("select count(*) from Test.Dept",myConn);
myConn.Open();
Console.WriteLine(myCommand.ExecuteScalar());
myConn.Close();
myMonitor.TraceEvent -= new MonitorEventHandler(OnEvent);
Console.ReadLine();
}
Sub OnEvent(ByVal sender As Object, ByVal e As MonitorEventArgs)
If e.TracePoint = MonitorTracePoint.BeforeEvent Then
Console.WriteLine("Description: " & e.Description)
Console.WriteLine(" Extra info: " & e.ExtraInfo)
End If
End Sub
<MTAThread()> _
Sub Main()
Dim myConn As SqlConnection = New SqlConnection( _
"User Id=sa;Server=localhost;Initial Catalog=Test;")
Dim myMonitor As SqlMonitor = New SqlMonitor
AddHandler myMonitor.TraceEvent, AddressOf OnEvent
myMonitor.IsActive = True
Dim myCommand As SqlCommand = New SqlCommand("select count(*) from Test.Dept", myConn)
myConn.Open()
Console.WriteLine(myCommand.ExecuteScalar())
myConn.Close()
RemoveHandler myMonitor.TraceEvent, AddressOf OnEvent
Console.ReadLine()
End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2