Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add and remove event handlers with ADO.NET RowUpdated

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use ADO.NET RowUpdated to add and delete event handlers", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use ADO.NET RowUpdated to add and delete event handlers" bar!

RowUpdated is thrown before any updates to a row in DataSet are processed in the data source. RowUpdated is thrown after any updates to a row in DataSet are processed in the data source.

Therefore, you can use RowUpdating to modify the update behavior before it occurs, provide additional processing when the update will occur, retain references to updated rows, cancel the current update and schedule it for batch processing later, and so on. RowUpdated is useful for responding to errors and exceptions that occur during updates. You can add error messages to DataSet, retry logic, and so on.

The RowUpdatingEventArgs and RowUpdatedEventArgs parameters passed to the RowUpdating and RowUpdated events include: the Command property, which references the Command object used to perform the update; the Row property, which references the DataRow object containing the update information; the StatementType property, which indicates the type of update being performed; the TableMapping, if applicable, and the Status of the operation.

You can use the Status property to determine whether an error occurred during the operation and, if necessary, to control the actions performed on the current row and the result row. When this event occurs, the Status property will be Continue or ErrorsOccurred. The following table shows that you can set the Status property to the value of to control subsequent actions during the update process. If the Status property is set to ErrorsOccurred, an exception is thrown. You can control the exception that is thrown by setting the Errors property to the desired exception. If you use one of the other values of Status, you can prevent exceptions from being thrown.

You can also use the ContinueUpdateOnError property to handle errors for updated rows. If DataAdapter.ContinueUpdateOnError is true, when an update to a row causes an exception to be thrown, the text of the exception is placed in the RowError information for a particular line, and processing continues without throwing an exception. This enables you to respond to errors when the Update is complete; the opposite is the ADO.NET RowUpdated event, which enables you to respond to errors when they are encountered.

The following code example shows how to add and remove event handlers. The RowUpdating event handler writes a log of all deleted records with a timestamp. The ADO.NET RowUpdated event handler adds the error message to the RowError property of the row in DataSet, cancels the display of the exception, and continues processing (mirroring the behavior of ContinueUpdateOnError = true).

'Assumes that connection is a valid SqlConnection object. Dim custAdapter As SqlDataAdapter = New SqlDataAdapter (_ "SELECT CustomerID, CompanyName FROM Customers", connection) 'Add handlers. AddHandler custAdapter.RowUpdating, New SqlRowUpdatingEventHandler (_ AddressOf OnRowUpdating) AddHandler custAdapter.RowUpdated, New SqlRowUpdatedEventHandler (AddressOf OnRowUpdated) 'Set DataAdapter command properties, fill DataSet, and modify DataSet. CustAdapter.Update (custDS, "Customers") 'Remove handlers. RemoveHandler custAdapter.RowUpdating, _ New SqlRowUpdatingEventHandler (AddressOf OnRowUpdating) RemoveHandler custAdapter.RowUpdated, _ New SqlRowUpdatedEventHandler (AddressOf OnRowUpdated) Private Shared Sub OnRowUpdating (sender As Object, _ args As SqlRowUpdatingEventArgs) If args.StatementType = StatementType.Delete Then Dim tw As System.IO.TextWriter = _ System.IO.File.AppendText ("Deletes.log") tw.WriteLine (_ "{0}: Customer {1} Deleted.", DateTime.Now, args.Row (_ "CustomerID") DataRowVersion.Original)) tw.Close () End If End Sub Private Shared Sub OnRowUpdated (_ sender As Object, args As SqlRowUpdatedEventArgs) If args.Status = UpdateStatus.ErrorsOccurred args.Status = UpdateStatus.SkipCurrentRow argsargs.Row.RowError = args.Errors.Message End If End Sub Thank you for reading The above is the content of "how to add and delete event handlers with ADO.NET RowUpdated". After the study of this article, I believe you have a deeper understanding of how to use ADO.NET RowUpdated to add and delete event handlers, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 271

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report