Export (0) Print
Expand All

SqlRowUpdatingEventArgs Class

Provides data for the RowUpdating event. This class cannot be inherited.

For a list of all members of this type, see SqlRowUpdatingEventArgs Members.

System.Object
   System.EventArgs
      System.Data.Common.RowUpdatingEventArgs
         System.Data.SqlClient.SqlRowUpdatingEventArgs

[Visual Basic]
NotInheritable Public Class SqlRowUpdatingEventArgs
   Inherits RowUpdatingEventArgs
[C#]
public sealed class SqlRowUpdatingEventArgs : RowUpdatingEventArgs
[C++]
public __gc __sealed class SqlRowUpdatingEventArgs : public
   RowUpdatingEventArgs
[JScript]
public class SqlRowUpdatingEventArgs extends RowUpdatingEventArgs

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The RowUpdating event is raised before an Update to a row.

When using Update, there are two events that occur for each data row updated. The order of execution is as follows:

  1. The values in the DataRow are moved to the parameter values.
  2. The OnRowUpdating event is raised.
  3. The command executes.
  4. If the command is set to FirstReturnedRecord, then the first returned result is placed in the DataRow.
  5. If there are output parameters, they are placed in the DataRow.
  6. The OnRowUpdated event is raised.
  7. AcceptChanges is called.

Example

[Visual Basic, C#, C++] The following example shows how to use both the RowUpdating and RowUpdated events.

[Visual Basic, C#, C++] The RowUpdating event returns this output:

[Visual Basic, C#, C++] event args: (command=System.Data.SqlClient.SQLCommand commandType=2 status=0)

[Visual Basic, C#, C++] The RowUpdated event returns this output:

[Visual Basic, C#, C++] event args: (command=System.Data.SqlClient.SQLCommand commandType=2 recordsAffected=1 row=System.Data.DataRow[37] status=0)

[Visual Basic] 
'Handler for RowUpdating event.
Protected Shared Sub OnRowUpdating(sender As Object, e As SqlRowUpdatingEventArgs)
    PrintEventArgs(e)
End Sub 'OnRowUpdating

'Handler for RowUpdated event.
Protected Shared Sub OnRowUpdated(sender As Object, e As SqlRowUpdatedEventArgs)
    PrintEventArgs(e)
End Sub 'OnRowUpdated

'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)
    
    'Create and fill DataSet (Select only first 5 rows.).
    Dim rDataSet As New DataSet()
    rAdapter.Fill(rDataSet, 0, 5, "Table")
    
    'Modify DataSet.
    Dim rTable As DataTable = rDataSet.Tables("Table")
    rTable.Rows(0)(1) = "new product"
    
    'Add handlers.
    AddHandler rAdapter.RowUpdating, AddressOf OnRowUpdating
    AddHandler rAdapter.RowUpdated, AddressOf OnRowUpdated
    
    'Update--this operation fires two events (RowUpdating and RowUpdated) for each changed row. 
    rAdapter.Update(rDataSet, "Table")
    
    'Remove handlers.
    RemoveHandler rAdapter.RowUpdating, AddressOf OnRowUpdating
    RemoveHandler rAdapter.RowUpdated, AddressOf OnRowUpdated
    Return 0
End Function 'Main

Overloads Protected 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 'PrintEventArgs


Overloads Protected 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 'PrintEventArgs

[C#] 
// handler for RowUpdating event
 protected static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e) {
    PrintEventArgs(e);
 }
 
 //Handler for RowUpdated event.
 protected static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs 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);
 
    //Create and fill DataSet (Select only first 5 rows.).
    DataSet rDataSet = new DataSet();
    rAdapter.Fill(rDataSet, 0, 5, "Table");
 
    //Modify DataSet.
    DataTable rTable = rDataSet.Tables["Table"];
    rTable.Rows[0][1] = "new product";
 
    //Add handlers.
    rAdapter.RowUpdating += new SqlRowUpdatingEventHandler( OnRowUpdating );
    rAdapter.RowUpdated += new SqlRowUpdatedEventHandler( OnRowUpdated );
 
    //Update--this operation fires two events (RowUpdating and RowUpdated) for each changed row. 
    rAdapter.Update(rDataSet, "Table");
 
    //Remove handlers.
    rAdapter.RowUpdating -= new SqlRowUpdatingEventHandler( OnRowUpdating );
    rAdapter.RowUpdated -= new SqlRowUpdatedEventHandler( OnRowUpdated );
    return 0;
 }
 
 protected static void PrintEventArgs(SqlRowUpdatingEventArgs args) {
    Console.WriteLine("OnRowUpdating");
    Console.WriteLine("  event args: ("+
           " command=" + args.Command + 
           " commandType=" + args.StatementType + 
           " status=" + args.Status + ")");
 }
 
 protected static void PrintEventArgs(SqlRowUpdatedEventArgs args) {
    Console.WriteLine("OnRowUpdated");
    Console.WriteLine("  event args: ("+
           " command=" + args.Command +
           " commandType=" + args.StatementType + 
           " recordsAffected=" + args.RecordsAffected + 
           " status=" + args.Status + ")");
 }

[C++] 
// handler for RowUpdating event
protected:
 static void OnRowUpdating(Object* sender, SqlRowUpdatingEventArgs* e) {
    PrintEventArgs(e);
 }
 
 //Handler for RowUpdated event.
protected:
 static void OnRowUpdated(Object* sender, SqlRowUpdatedEventArgs* e) {
    PrintEventArgs(e);
 }
 
public:
 static int 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);
 
    //Create and fill DataSet (Select only first 5 rows.).
    DataSet* rDataSet = new DataSet();
    rAdapter->Fill(rDataSet, 0, 5, S"Table");
 
    //Modify DataSet.
    DataTable* rTable = rDataSet->Tables->Item[S"Table"];
    rTable->Rows->Item[0]->Item[1] = S"new product";
 
    //Add handlers.
    rAdapter->RowUpdating += new SqlRowUpdatingEventHandler(0,  OnRowUpdating );
    rAdapter->RowUpdated += new SqlRowUpdatedEventHandler(0,  OnRowUpdated );
 
    //Update--this operation fires two events (RowUpdating and RowUpdated) for each changed row. 
    rAdapter->Update(rDataSet, S"Table");
 
    //Remove handlers.
    rAdapter->RowUpdating -= new SqlRowUpdatingEventHandler(0,  OnRowUpdating );
    rAdapter->RowUpdated -= new SqlRowUpdatedEventHandler(0,  OnRowUpdated );
    return 0;
 }
 
protected:
 static void PrintEventArgs(SqlRowUpdatingEventArgs* args) {
    Console::WriteLine(S"OnRowUpdating");
    Console::WriteLine(String::Format( S"  event args: ( command={0} commandType={1} status={2})",
       args->Command, __box(args->StatementType), __box(args->Status)));
 }
 
 static void PrintEventArgs(SqlRowUpdatedEventArgs* args) {
    Console::WriteLine(S"OnRowUpdated");
    Console::WriteLine(String::Concat(
       String::Format( S"  event args: ( command={0} commandType={1}", args->Command, __box(args->StatementType)),
       String::Format( S" recordsAffected={2} status={3})", __box(args->RecordsAffected), __box(args->Status))));
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data.SqlClient

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

SqlRowUpdatingEventArgs Members | System.Data.SqlClient Namespace

Show:
© 2014 Microsoft