共用方式為


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.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)

適用於