RowUpdating 事件
發生在呼叫 Update 的期間,針對資料來源執行更新命令之前。嘗試進行更新,因而引發這個事件。
命名空間: System.Data.SqlServerCe
組件: System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)
語法
'宣告
Public Event RowUpdating As SqlCeRowUpdatingEventHandler
'用途
Dim instance As SqlCeDataAdapter
Dim handler As SqlCeRowUpdatingEventHandler
AddHandler instance.RowUpdating, handler
public event SqlCeRowUpdatingEventHandler RowUpdating
public:
event SqlCeRowUpdatingEventHandler^ RowUpdating {
void add (SqlCeRowUpdatingEventHandler^ value);
void remove (SqlCeRowUpdatingEventHandler^ value);
}
member RowUpdating : IEvent<SqlCeRowUpdatingEventHandler,
SqlCeRowUpdatingEventArgs>
JScript 支援使用事件,但不支援新的宣告。
備註
使用 Update 時,每個更新的資料列會發生兩個事件。執行的順序如下:
DataRow 中的值會移至參數值。
引發 OnRowUpdating 事件。
執行命令。
如果此命令設定為 FirstReturnedRecord,則傳回的第一個結果會放置在 DataRow 中。
引發 OnRowUpdated 事件。
呼叫 AcceptChanges。
範例
下列範例會顯示使用中的 RowUpdating 和 RowUpdated 事件。
Public Sub Snippet5()
' Create DataAdapter
'
Dim adp As New SqlCeDataAdapter("SELECT * FROM products", "Data Source = MyDatabase.sdf")
Dim cb As New SqlCeCommandBuilder(adp)
' Create and fill the dataset (select only first 5 rows)
'
Dim ds As New DataSet()
adp.Fill(ds, 0, 5, "Table")
' Modify dataSet
'
Dim table As DataTable = ds.Tables("Table")
table.Rows(1)("Product Name") = "Asian Chai"
' Add handlers
'
AddHandler adp.RowUpdating, AddressOf OnRowUpdating
AddHandler adp.RowUpdated, AddressOf OnRowUpdated
' Update, this operation fires two events (RowUpdating/RowUpdated)
'
adp.Update(ds, "Table")
' Remove handlers
'
RemoveHandler adp.RowUpdating, AddressOf OnRowUpdating
RemoveHandler adp.RowUpdated, AddressOf OnRowUpdated
End Sub 'Snippet5
Private Shared Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlCeRowUpdatingEventArgs)
Console.WriteLine("OnRowUpdating")
Console.WriteLine(e.Command.CommandText)
Console.WriteLine(e.StatementType)
Console.WriteLine(e.Status)
End Sub 'OnRowUpdating
Private Shared Sub OnRowUpdated(ByVal sender As Object, ByVal e As SqlCeRowUpdatedEventArgs)
Console.WriteLine("OnRowUpdated")
Console.WriteLine(e.Command.CommandText)
Console.WriteLine(e.StatementType)
Console.WriteLine(e.Status)
End Sub 'OnRowUpdated
public void Snippet5()
{
// Create DataAdapter
//
SqlCeDataAdapter adp = new SqlCeDataAdapter(
"SELECT * FROM products",
"Data Source = MyDatabase.sdf");
SqlCeCommandBuilder cb = new SqlCeCommandBuilder(adp);
// Create and fill the dataset (select only first 5 rows)
//
DataSet ds = new DataSet();
adp.Fill(ds, 0, 5, "Table");
// Modify dataSet
//
DataTable table = ds.Tables["Table"];
table.Rows[1]["Product Name"] = "Asian Chai";
// Add handlers
//
adp.RowUpdating += new SqlCeRowUpdatingEventHandler(OnRowUpdating);
adp.RowUpdated += new SqlCeRowUpdatedEventHandler(OnRowUpdated);
// Update, this operation fires two events (RowUpdating/RowUpdated)
//
adp.Update(ds, "Table");
// Remove handlers
//
adp.RowUpdating -= new SqlCeRowUpdatingEventHandler(OnRowUpdating);
adp.RowUpdated -= new SqlCeRowUpdatedEventHandler(OnRowUpdated);
}
private static void OnRowUpdating(object sender, SqlCeRowUpdatingEventArgs e)
{
Console.WriteLine("OnRowUpdating");
Console.WriteLine(e.Command.CommandText);
Console.WriteLine(e.StatementType);
Console.WriteLine(e.Status);
}
private static void OnRowUpdated(object sender, SqlCeRowUpdatedEventArgs e)
{
Console.WriteLine("OnRowUpdated");
Console.WriteLine(e.Command.CommandText);
Console.WriteLine(e.StatementType);
Console.WriteLine(e.Status);
}