Register two SqlConnection change events
using System;
using System.Data;
using System.Data.SqlClient;
class SqlDemo {
static void Main(){
string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
SqlConnection cn = new SqlConnection(connString);
cn.StateChange += new StateChangeEventHandler(CnStateChange);
cn.StateChange += new StateChangeEventHandler(CnStateChange2);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT LastName, FirstName FROM Employee";
cmd.Connection = cn;
try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
Console.WriteLine(dr.GetString(0) + "-" + dr.GetString(1));
}
}
catch(SqlException ex)
{
Console.WriteLine (ex.Message);
}
finally
{
cn.Close();
}
}
static void CnStateChange2(object sender, StateChangeEventArgs ev)
{
Console.WriteLine("------------------------------");
Console.WriteLine("Entering Second Statechange EventHandler");
Console.WriteLine("Sender = " + sender.ToString());
Console.WriteLine("Original State = " + ev.OriginalState.ToString());
Console.WriteLine("Current State = " + ev.CurrentState.ToString());
Console.WriteLine("Exiting Second StateChange EventHandler");
Console.WriteLine("------------------------------");
}
static void CnStateChange(object sender, StateChangeEventArgs ev)
{
Console.WriteLine("------------------------------");
Console.WriteLine("Entering StateChange EventHandler");
Console.WriteLine("Sender = "+ sender.ToString());
Console.WriteLine("Original State = "+ ev.OriginalState.ToString());
Console.WriteLine("Current State = "+ ev.CurrentState.ToString());
Console.WriteLine("Exiting StateChange EventHandler");
Console.WriteLine("------------------------------");
}
}
|
HTML code for linking to this page:
Related in same category :
-
-
-
|