How to use the StateChange event : SQL Events « Database ADO.net « C# / C Sharp






How to use the StateChange event

using System;
using System.Data;
using System.Data.SqlClient;

class StateChange
{
  public static void StateChangeHandler(object mySender, StateChangeEventArgs myEvent)
  {
    Console.WriteLine("mySqlConnection State has changed from "+myEvent.OriginalState + "to "+myEvent.CurrentState);
  }

  public static void Main(){
    SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

    mySqlConnection.StateChange +=new StateChangeEventHandler(StateChangeHandler);

    Console.WriteLine("Calling mySqlConnection.Open()");
    mySqlConnection.Open();

    Console.WriteLine("Calling mySqlConnection.Close()");
    mySqlConnection.Close();
  }
}


           
       








Related examples in the same category

1.How to use the InfoMessage event
2.On row updating and updated event
3.Register two SqlConnection change events