Listen to Connection state changed event : SqlConnection Event « ADO.Net « C# / CSharp Tutorial






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

    class Program
    {
        static void Main(string[] args)
        {
            string sqlConnectString = "Data Source=(local);Integrated security=SSPI;Initial Catalog=AdventureWorks;";

            SqlConnection connection = new SqlConnection(  );

            connection.StateChange += new StateChangeEventHandler(connection_StateChange);

            connection.ConnectionString = sqlConnectString +
                "Connection Timeout=15;Connection Lifetime=0;" +
                "Min Pool Size=0;Max Pool Size=100;Pooling=true;";

            Console.WriteLine("Connection string = {0}",connection.ConnectionString);
            connection.Open();
            connection.Close();

            connection.ConnectionString = sqlConnectString +
                "Connection Timeout=30;Connection Lifetime=0;" +
                "Min Pool Size=0;Max Pool Size=200;Pooling=true;";

            Console.WriteLine("\nConnection string = {0}",connection.ConnectionString);
            connection.Open();
            connection.Close();
        }
        static void connection_StateChange(object sender, StateChangeEventArgs e)
        {
            Console.WriteLine("\tOriginalState = {0}", e.OriginalState.ToString());
            Console.WriteLine("\tCurrentState = {0}", e.CurrentState.ToString());
        }
    }








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