ADO.NET events can be used to notify subscribers of significant actions that have occurred in a database. The following example demonstrates using ADO.NET events.
using System;
using System.Data;
using System.Data.SqlClient;
namespace Client.Chapter_13___ADO.NET
{
class UsingADONETEvents
{
static void Main(string[] args)
{
SqlConnection MyConnection =
new SqlConnection(@"Data Source=(local);"
+ "Initial Catalog = CaseManager;"
+ "Integrated Security=true");
MyConnection.StateChange +=
new StateChangeEventHandler(OnStateChange);
MyConnection.Open(); //Trigger Open event
MyConnection.Close();
}
//This method gets called when the state changes
public static void OnStateChange(object sender,
System.Data.StateChangeEventArgs e)
{
Console.WriteLine("Connection State Chnaged: {0}",
((SqlConnection)sender).State);
}
}
}