次の方法で共有


StateChangeEventArgs クラス

.NET Framework データ プロバイダの状態変化イベントにデータを提供します。

この型のすべてのメンバの一覧については、StateChangeEventArgs メンバ を参照してください。

System.Object
   System.EventArgs
      System.Data.StateChangeEventArgs

NotInheritable Public Class StateChangeEventArgs
   Inherits EventArgs
[C#]
public sealed class StateChangeEventArgs : EventArgs
[C++]
public __gc __sealed class StateChangeEventArgs : public EventArgs
[JScript]
public class StateChangeEventArgs extends EventArgs

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

このデータは、 OleDbConnectionStateChange プロパティと SqlConnectionStateChange プロパティによって使用されます。

使用例

[Visual Basic, C#, C++] SqlConnection クラス内で StateChange イベントを使用する方法を次の例に示します。

 
' handler for StateChange event
Protected Shared Sub OnStateChange(sender As Object, e As StateChangeEventArgs)
    PrintEventArgs(e)
End Sub 'OnStateChange

'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 = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"
    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 result it fires several StateChange events
    rAdapter.Fill(rDataSet, 0, 5, "Table")
    Return 0
End Function 'Main

Protected Shared Sub PrintEventArgs(args As StateChangeEventArgs)
    Console.WriteLine("OnStateChange")
    Console.WriteLine("  event args: (" & _
                     "originalState=" & _
                     args.OriginalState & _
                     " currentState=" & _
                     args.CurrentState & _
                     ")")
End Sub 'PrintEventArgs
End Class 'Form1

[C#] 
// handler for StateChange event
 protected static void OnStateChange(object sender, StateChangeEventArgs e)
 {
    PrintEventArgs(e);
 }
 
 public static int Main(String[] args)
 {
    const string CONNECTION_STRING = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    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 result it fires several StateChange events
    rAdapter.Fill(rDataSet, 0, 5, "Table");
 
    return 0;
 }
 
 protected static void PrintEventArgs(StateChangeEventArgs args)
 {
    Console.WriteLine("OnStateChange");
    Console.WriteLine("  event args: ("+
           "originalState=" + args.OriginalState +
           " currentState=" + args.CurrentState +")");
 }

[C++] 
// handler for StateChange event
protected:
 static void OnStateChange(Object* /*sender*/, StateChangeEventArgs* e)
 {
    PrintEventArgs(e);
 }

public: 
 static void main()
 {
    String* CONNECTION_STRING = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    String* SELECT_ALL = S"select * from Products";
 
    // create DataAdapter
    SqlDataAdapter* rAdapter = new SqlDataAdapter(SELECT_ALL, CONNECTION_STRING);
 
    // add handlers
    rAdapter->SelectCommand->Connection->StateChange += new StateChangeEventHandler(0, OnStateChange);
 
    // create dataset
    DataSet* rDataSet = new DataSet();
 
    // fill dataset, 
    // DataAdapter will open connection, load data and restore connection state - close connection.
    // as result it fires several StateChange events
    rAdapter->Fill(rDataSet, 0, 5, S"Table");
 }
 
protected:
 static void PrintEventArgs(StateChangeEventArgs* args)
 {
    Console::WriteLine(S"OnStateChange");
    Console::WriteLine(S"  event args: (originalState={0} currentState={1})",
       __box(args->OriginalState), __box(args->CurrentState));
 }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Data

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System.Data (System.Data.dll 内)

参照

StateChangeEventArgs メンバ | System.Data 名前空間