Using ADO.NET Events : SqlConnection Event « ADO.Net « C# / CSharp Tutorial






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);
  }
}
Connection State Chnaged: Open
Connection State Chnaged: Closed








32.14.SqlConnection Event
32.14.1.Using ADO.NET Events
32.14.2.how to use the InfoMessage event
32.14.3.Listen to Connection state changed event