// handler for StateChange event
protected static void OnStateChange ( object sender,
StateChangeEventArgs e ) {
PrintEventArgs ( e );
}
public static int Main ( String [ ] args ) {
const string CONNECTION_STRING =
"Server = LocalHost;user id = sa;timeout = 30;database = Northwind";
const string SELECT_ALL = "SELECT * FROM Products";
// create DataAdapter
SqlDataAdapter rAdapter = new SqlDataAdapter
( SELECT_ALL, CONNECTION_STRING );
// add handlers
rAdapter.SelectCommand.Connection.StateChange + =
new StateChangeEventHandler ( OnStateChange );
// create dataset
DataSet rDataSet = new DataSet ( );
// fill dataset: DataAdapter will open connection, load data and
// restore connection state ( close connection ).
// as a result it fires several StateChange events
rAdapter.Fill ( rDataSet, 0, 5, "Table" );
return 0;
}
protected static void PrintEventArgs ( StateChangeEventArgs args ) {
Response.Write ( "OnStateChange" );
Response.Write ( " Event args: ( " +
" originalState = " + args.OriginalState +
" currentState = " + args.CurrentState + " ) " );
}
' handler for StateChange event
Protected Shared Sub OnStateChange ( sender As Object, _
e As StateChangeEventArgs )
PrintEventArgs ( e )
End Sub
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main ( )
System.Environment.ExitCode = _
Main ( System.Environment.GetCommandLineArgs ( ) )
End Sub
Overloads Public Shared Function Main ( args ( ) As String ) As Integer
Const CONNECTION_STRING As String = _
"Server = LocalHost;user id = sa;timeout = 30;database = Northwind"
Const SELECT_ALL As String = _
"SELECT * FROM Products"
' create DataAdapter
Dim rAdapter As New SqlDataAdapter ( SELECT_ALL, CONNECTION_STRING )
' add handlers
AddHandler rAdapter.SelectCommand.Connection.StateChange, _
AddressOf OnStateChange
' create dataset
Dim rDataSet As New DataSet ( )
' fill dataset: DataAdapter will open connection, load data and
' restore connection state ( close connection ).
' as a result it fires several StateChange events
rAdapter.Fill ( rDataSet, 0, 5, "Table" )
Return 0
End Function 'Main
Protected Shared Sub PrintEventArgs ( args As StateChangeEventArgs )
Response.Write ( "OnStateChange" )
Response.Write ( " Event args: ( " & _
" originalState = " & args.OriginalState & _
" currentState = " & args.CurrentState & " ) " )
End Sub |