The output from this program is as follows:
count = 1
Milliseconds = 101
mySqlConnection.State = Open
count = 2
Milliseconds = 0
mySqlConnection.State = Open
count = 3
Milliseconds = 0
mySqlConnection.State = Open
count = 4
Milliseconds = 0
mySqlConnection.State = Open
count = 5
Milliseconds = 0
mySqlConnection.State = Open
count = 6
Milliseconds = 0
mySqlConnection.State = Open
count = 7
Milliseconds = 0
mySqlConnection.State = Open
count = 8
Milliseconds = 0
mySqlConnection.State = Open
count = 9
Milliseconds = 0
mySqlConnection.State = Open
count = 10
Milliseconds = 0
mySqlConnection.State = Open
An example of using the State property would be to check if your Connection object is
currently open before calling its Open() method. You might need to do that if you have a
complex application and you're using a Connection object created somewhere else in the
application: you might not know the current state of that Connection object and you don't
want to call the Open() method on an already open Connection because that will raise an
exception.
The following example uses the State property to check if mySqlConnection is closed
before opening it:
if (mySqlConnection.State == ConnectionState.Closed)
{
mySqlConnection.Open();
}
As you'll learn in the next section
, you can use the StateChange event to monitor changes
in a Connection object's state.
Using Connection Events
The Connection classes have two useful events: StateChange and InfoMessage. You'll
see how to use these events next.
The StateChange Event
The StateChange event fires when the state of your connection is changed, and you can
use this event to monitor changes in the state of your Connection object.
The method that handles an event is known as an event handler. You call this method
when a particular event is fired. All event handler methods must return void and accept
two parameters. The first parameter is an object (of the class System.Object), and it
represents the object that raises the event.
Note The System.Object class acts as the base class for all classes. In other words, all
classes are ultimately derived from the System.Object class.
The second parameter is an object of a class that is derived from the System.EventArgs
StateChange.cs illustrates how to use the StateChange event
*/
using System;
using System.Data;
using System.Data.SqlClient;
class StateChange
{
// define the StateChangeHandler() method to handle the
// StateChange event
public static void StateChangeHandler(
object mySender, StateChangeEventArgs myEvent
)
{
Console.WriteLine(
"mySqlConnection State has changed from "+
myEvent.OriginalState + "to "+
myEvent.CurrentState
);
}
public static void Main()
{
// create a SqlConnection object
SqlConnection mySqlConnection =
new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
// monitor the StateChange event using the StateChangeHandler() method
mySqlConnection.StateChange +=
Notice the use of the Errors collection to display the message:
public static void InfoMessageHandler(
object mySender, SqlInfoMessageEventArgs myEvent
)
{
Console.WriteLine(
"The following message was produced:\n" +
myEvent.Errors[0]
);
}