SqlRowUpdatingEventArgs 类

RowUpdating 事件提供数据。

**命名空间:**System.Data.SqlClient
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public NotInheritable Class SqlRowUpdatingEventArgs
    Inherits RowUpdatingEventArgs
用法
Dim instance As SqlRowUpdatingEventArgs
public sealed class SqlRowUpdatingEventArgs : RowUpdatingEventArgs
public ref class SqlRowUpdatingEventArgs sealed : public RowUpdatingEventArgs
public final class SqlRowUpdatingEventArgs extends RowUpdatingEventArgs
public final class SqlRowUpdatingEventArgs extends RowUpdatingEventArgs

备注

在对行 Update 之前引发 RowUpdating 事件。

当使用 Update 时,每个更新的数据行都发生两个事件。执行顺序如下:

  1. DataRow 中的值移至参数值。

  2. 引发 OnRowUpdating 事件。

  3. 执行命令。

  4. 如果该命令设置为 FirstReturnedRecord,返回的第一项结果将放置在 DataRow 中。

  5. 如果存在输出参数,它们将被放在 DataRow 中。

  6. 引发 OnRowUpdated 事件。

  7. 调用 AcceptChanges

示例

下面的示例显示如何使用 RowUpdatingRowUpdated 事件。

RowUpdating 事件返回下面的输出:

event args: (command=System.Data.SqlClient.SQLCommand commandType=2 status=0)

RowUpdated 事件返回下面的输出:

event args: (command=System.Data.SqlClient.SQLCommand commandType=2 recordsAffected=1 row=System.Data.DataRow[37] status=0)

'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 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 

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 
// 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 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;
}
 
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 + ")");
}

继承层次结构

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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

SqlRowUpdatingEventArgs 成员
System.Data.SqlClient 命名空间