共用方式為


StateChangeEventArgs 類別

定義

提供 .NET 資料提供者狀態變更事件的資料。

public ref class StateChangeEventArgs sealed : EventArgs
public sealed class StateChangeEventArgs : EventArgs
type StateChangeEventArgs = class
    inherit EventArgs
Public NotInheritable Class StateChangeEventArgs
Inherits EventArgs
繼承
StateChangeEventArgs

範例

下列範例示範如何在 StateChange 類別中使用 SqlConnection 事件。

// Handler for OnStateChange event.
protected static void OnStateChange(object sender,
    StateChangeEventArgs e)
{
    PrintEventArgs(e);
}

static void Main()
{
    FillDataSet();
}

static private void FillDataSet()
{
    string connectionString = GetConnectionString();
    string queryString =
        "SELECT ProductID, UnitPrice from dbo.Products;";

    // Create a DataAdapter.
    using (SqlDataAdapter dataAdapter =
               new SqlDataAdapter(queryString, connectionString))
    {

        // Add the handlers.
        dataAdapter.SelectCommand.Connection.StateChange
            += new StateChangeEventHandler(OnStateChange);

        // Create a DataSet.
        DataSet dataSet = new DataSet();

        // Fill the DataSet, which fires several StateChange events.
        dataAdapter.Fill(dataSet, 0, 5, "Table");
    }
}

protected static void PrintEventArgs(StateChangeEventArgs args)
{
    Console.WriteLine("StateChangeEventArgs");
    Console.WriteLine("  OriginalState= {0} CurrentState= {1}",
        args.OriginalState, args.CurrentState);
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code,
    // you can retrieve it from a configuration file.
    return "Data Source=(local);Initial Catalog=Northwind;"
        + "Integrated Security=true";
}
' Handler for the OnStateChange event.
Private Sub OnStateChange(ByVal sender As Object, _
    ByVal e As StateChangeEventArgs)
    PrintEventArgs(e)
End Sub

Sub Main()
    FillDataSet()
End Sub

Private Sub FillDataSet()
    Dim connectionString As String = GetConnectionString()
    Dim queryString As String = _
        "SELECT ProductID, UnitPrice from dbo.Products;"

    ' Create a DataAdapter.
    Using dataAdapter As New SqlDataAdapter( _
        queryString, connectionString)

        ' Add the handlers.
        AddHandler dataAdapter.SelectCommand.Connection.StateChange, _
            AddressOf OnStateChange

        ' Create a DataSet.
        Dim dataSet As New DataSet()

        ' Fill the DataSet, which fires several StateChange events.
        dataAdapter.Fill(dataSet, 0, 5, "Table")
    End Using
End Sub

Private Sub PrintEventArgs(ByVal args As StateChangeEventArgs)
    Console.WriteLine("StateChangeEventArgs")
    Console.WriteLine("  OriginalState= {0} CurrentState= {1}", _
        args.OriginalState, args.CurrentState)
End Sub

Private Function GetConnectionString() As String
    ' To avoid storing the connection string in your code,  
    ' you can retrieve it from a configuration file.
    Return "Data Source=(local);Initial Catalog=Northwind;" _
       & "Integrated Security=true;"
End Function

備註

數據是由 StateChange 的屬性和 StateChange 的屬性使用SqlConnectionOleDbConnection

建構函式

StateChangeEventArgs(ConnectionState, ConnectionState)

在提供物件的原始狀態和目前狀態時,初始化 StateChangeEventArgs 類別的新執行個體。

屬性

CurrentState

取得連線的新狀態。 當引發事件時,連接物件將會處於新的狀態中。

OriginalState

取得連線的原始狀態。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於