SqlConnection.StateChange : SqlConnection « System.Data.SqlClient « C# / C Sharp by API






SqlConnection.StateChange

 


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class MainClass{
   public static void Main(){
    SqlConnection cn = new SqlConnection();

    try
    {
      cn.StateChange += new StateChangeEventHandler(StateChange);
      cn.InfoMessage += new SqlInfoMessageEventHandler(InfoMessage);

      cn.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
      cn.Open();
      cn.ChangeDatabase("Biblio");

    }
    catch( Exception ex )
    {
      Console.WriteLine(ex.Message);
    }
    finally
    {
      // Unsubscribe from the Events we subscribed to.
      cn.StateChange -= new StateChangeEventHandler(StateChange);
      cn.InfoMessage -= new SqlInfoMessageEventHandler(InfoMessage);
    }
   }
  private static void StateChange(object sender, System.Data.StateChangeEventArgs e)
  {
    Console.WriteLine("State was:" + e.OriginalState.ToString() + ". New state = " + e.CurrentState.ToString() );
  }
  
  private static void InfoMessage( object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
  {
    int i;
    for (i=0; i < e.Errors.Count; i++ )
    {
      Console.WriteLine(e.Errors[i].Message);
    }
  }

}

   
  








Related examples in the same category

1.new SqlConnection(String connection)
2.SqlConnection.BeginTransaction()
3.SqlConnection.ChangeDatabase
4.SqlConnection.ConnectionString
5.SqlConnection.ConnectionTimeout
6.SqlConnection.DataSource
7.SqlConnection.Database
8.SqlConnection.InfoMessage
9.SqlConnection.PacketSize
10.SqlConnection.ServerVersion
11.SqlConnection.State
12.SqlConnection.WorkstationId