using System;
using System.Data;
using System.Data.SqlClient;
class MainClass
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");
MyConnection.StateChange += new StateChangeEventHandler(OnStateChange);
MyConnection.Open(); //Trigger Open Event
MyConnection.Close();
}
public static void OnStateChange(object sender, System.Data.StateChangeEventArgs e)
{
Console.WriteLine("Connection State Chnaged: {0}", ((SqlConnection)sender).State);
}
}
Output
Connection State Chnaged: Open
Connection State Chnaged: Closed