SqlRowUpdatingEventArgs 类

定义

RowUpdating 事件提供数据。

public ref class SqlRowUpdatingEventArgs sealed : System::Data::Common::RowUpdatingEventArgs
public sealed class SqlRowUpdatingEventArgs : System.Data.Common.RowUpdatingEventArgs
type SqlRowUpdatingEventArgs = class
    inherit RowUpdatingEventArgs
Public NotInheritable Class SqlRowUpdatingEventArgs
Inherits RowUpdatingEventArgs
继承
SqlRowUpdatingEventArgs

示例

以下示例演示如何同时使用 RowUpdatingRowUpdated 事件。

事件 RowUpdating 返回以下输出:

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

事件 RowUpdated 返回以下输出:

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

using Microsoft.Data.SqlClient;
using System.Xml;
using System.Data.Common;
using System.Windows.Forms;

public class Form1 : Form
{
    private DataSet DataSet1;
    private DataGrid dataGrid1;


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

注解

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

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

  1. 中的 DataRow 值将移动到参数值。

  2. 引发 OnRowUpdating 事件。

  3. 命令将执行。

  4. 如果命令设置为 FirstReturnedRecord,并且第一个返回的结果将放置在 中 DataRow

  5. 如果有输出参数,则它们放置在 中 DataRow

  6. 引发 OnRowUpdated 事件。

  7. 调用 AcceptChanges

构造函数

SqlRowUpdatingEventArgs(DataRow, IDbCommand, StatementType, DataTableMapping)

初始化 SqlRowUpdatingEventArgs 类的新实例。

属性

Command

获取或设置进行 SqlCommand 时执行的 Update(DataSet)

适用于