SqlRowUpdatedEventArgs Classe

Definizione

Fornisce i dati per l'evento RowUpdated.

public ref class SqlRowUpdatedEventArgs sealed : System::Data::Common::RowUpdatedEventArgs
public sealed class SqlRowUpdatedEventArgs : System.Data.Common.RowUpdatedEventArgs
type SqlRowUpdatedEventArgs = class
    inherit RowUpdatedEventArgs
Public NotInheritable Class SqlRowUpdatedEventArgs
Inherits RowUpdatedEventArgs
Ereditarietà
SqlRowUpdatedEventArgs

Esempio

Nell'esempio seguente viene illustrato come usare gli RowUpdating eventi e RowUpdated .

L'evento RowUpdating restituisce questo output:

Argomenti evento: (command=System.Data.SqlClient.SqlCommand commandType=2 status=0)

L'evento RowUpdated restituisce questo output:

Argomenti evento: (command=System.Data.SqlClient.SqlCommand commandType=2 recordsAffected=1 row=System.Data.DataRow[37] status=0)

// handler for RowUpdating event
private static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
    PrintEventArgs(e);
}

// handler for RowUpdated event
private static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
    PrintEventArgs(e);
}

public static int Main()
{
    const string connectionString =
              "Integrated Security=SSPI;database=Northwind;server=MSSQL1";
    const string queryString = "SELECT * FROM Products";

    // create DataAdapter
    SqlDataAdapter adapter = new SqlDataAdapter(queryString, connectionString);
    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

    // Create and fill DataSet (select only first 5 rows)
    DataSet dataSet = new DataSet();
    adapter.Fill(dataSet, 0, 5, "Table");

    // Modify DataSet
    DataTable table = dataSet.Tables["Table"];
    table.Rows[0][1] = "new product";

    // add handlers
    adapter.RowUpdating += new SqlRowUpdatingEventHandler( OnRowUpdating );
    adapter.RowUpdated += new SqlRowUpdatedEventHandler( OnRowUpdated );

    // update, this operation fires two events
    // (RowUpdating/RowUpdated) per changed row
    adapter.Update(dataSet, "Table");

    // remove handlers
    adapter.RowUpdating -= new SqlRowUpdatingEventHandler( OnRowUpdating );
    adapter.RowUpdated -= new SqlRowUpdatedEventHandler( OnRowUpdated );
    return 0;
}

private static void PrintEventArgs(SqlRowUpdatingEventArgs args)
{
    Console.WriteLine("OnRowUpdating");
    Console.WriteLine("  event args: ("+
        " command=" + args.Command +
        " commandType=" + args.StatementType +
        " status=" + args.Status + ")");
}

private static void PrintEventArgs(SqlRowUpdatedEventArgs args)
{
    Console.WriteLine("OnRowUpdated");
    Console.WriteLine( "  event args: ("+
        " command=" + args.Command +
        " commandType=" + args.StatementType +
        " recordsAffected=" + args.RecordsAffected +
        " status=" + args.Status + ")");
}
    ' handler for RowUpdating event
    Private Shared Sub OnRowUpdating(sender As Object, e As SqlRowUpdatingEventArgs)
        PrintEventArgs(e)
    End Sub 

    ' handler for RowUpdated event
    Private Shared Sub OnRowUpdated(sender As Object, e As SqlRowUpdatedEventArgs)
        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 connectionString As String = _
            "Integrated Security=SSPI;database=Northwind;server=MSSQL1"
        Const queryString As String = "SELECT * FROM Products"
        
        ' create DataAdapter
        Dim adapter As New SqlDataAdapter(queryString, connectionString)
        Dim builder As New SqlCommandBuilder(adapter)
        
        ' Create and fill DataSet (select only first 5 rows)
        Dim dataSet As New DataSet()
        adapter.Fill(dataSet, 0, 5, "Table")
        
        ' Modify DataSet
        Dim table As DataTable = dataSet.Tables("Table")
        table.Rows(0)(1) = "new product"
        
        ' add handlers
        AddHandler adapter.RowUpdating, AddressOf OnRowUpdating
        AddHandler adapter.RowUpdated, AddressOf OnRowUpdated
        
        ' update, this operation fires two events 
        '(RowUpdating/RowUpdated) per changed row 
        adapter.Update(dataSet, "Table")
        
        ' remove handlers
        RemoveHandler adapter.RowUpdating, AddressOf OnRowUpdating
        RemoveHandler adapter.RowUpdated, AddressOf OnRowUpdated
        Return 0
    End Function 
    
    
    Overloads Private Shared Sub PrintEventArgs(args As SqlRowUpdatingEventArgs)
        Console.WriteLine("OnRowUpdating")
        Console.WriteLine("  event args: (" & " command=" & args.Command.CommandText & _
           " commandType=" & args.StatementType & " status=" & args.Status & ")")
    End Sub 
    
    
    Overloads Private Shared Sub PrintEventArgs(args As SqlRowUpdatedEventArgs)
        Console.WriteLine("OnRowUpdated")
        Console.WriteLine("  event args: (" & " command=" & args.Command.CommandText & _
           " commandType=" & args.StatementType & " recordsAffected=" & _
           args.RecordsAffected & " status=" & args.Status & ")")
    End Sub 
End Class

Commenti

L'evento RowUpdated viene generato quando Update viene completato un oggetto a una riga.

Quando si usa Update, sono presenti due eventi che si verificano per ogni riga di dati aggiornata. L'ordine di esecuzione è il seguente:

  1. I valori in DataRow vengono spostati nei valori dei parametri.

  2. Viene generato l'evento OnRowUpdating.

  3. Il comando viene eseguito.

  4. Se il comando è impostato su FirstReturnedRecorde il primo risultato restituito viene inserito in DataRow.

  5. Se sono presenti parametri di output, vengono inseriti in DataRow.

  6. Viene generato l'evento OnRowUpdated.

  7. Chiamata del metodo AcceptChanges.

Costruttori

SqlRowUpdatedEventArgs(DataRow, IDbCommand, StatementType, DataTableMapping)

Inizializza una nuova istanza della classe SqlRowUpdatedEventArgs.

Proprietà

Command

Ottiene o imposta la classe SqlCommand eseguita quando viene chiamato il metodo Update(DataSet).

Errors

Ottiene tutti gli errori generati dal provider di dati .NET al momento dell'esecuzione del metodo Command.

(Ereditato da RowUpdatedEventArgs)
RecordsAffected

Ottiene il numero di righe modificate, inserite o eliminate dall'esecuzione dell'istruzione SQL.

(Ereditato da RowUpdatedEventArgs)
Row

Ottiene la classe DataRow inviata attraverso un metodo Update(DataSet).

(Ereditato da RowUpdatedEventArgs)
RowCount

Ottiene il numero di righe elaborate in un batch di record aggiornati.

(Ereditato da RowUpdatedEventArgs)
StatementType

Ottiene il tipo di istruzione SQL eseguita.

(Ereditato da RowUpdatedEventArgs)
Status

Ottiene l'oggetto UpdateStatus della proprietà Command.

(Ereditato da RowUpdatedEventArgs)
TableMapping

Ottiene la classe DataTableMapping inviata attraverso un metodo Update(DataSet).

(Ereditato da RowUpdatedEventArgs)

Metodi

CopyToRows(DataRow[])

Copia i riferimenti alle righe modificate nella matrice fornita.

(Ereditato da RowUpdatedEventArgs)
CopyToRows(DataRow[], Int32)

Copia i riferimenti alle righe modificate nella matrice fornita.

(Ereditato da RowUpdatedEventArgs)
Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche