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