Using ADO.NET Events : SqlConnection Event : ADO.Net C# Examples


C# Examples » ADO.Net » SqlConnection Event »

 

Using ADO.NET Events









    
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


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlConnection Event