Updating the database using DataAdapter and Datasets [C #]

Source: Internet
Author: User
The DataAdapter Update method is used to parse changes in the dataset back to the data source. Like the Fill method, the Update method uses an instance of the DataSet and an optional DataTable object or DataTable name as parameters. The dataset instance is a dataset that contains the changes made, and the DataTable identifies the table from which the changes were retrieved.
When the Update method is called, DataAdapter analyzes the changes made and executes the appropriate command (INSERT, Update, or DELETE). When DataAdapter encounters a change to DataRow, it uses InsertCommand, UpdateCommand, or DeleteCommand to handle the change. This allows you to maximize the performance of your ado.net application by specifying the command syntax at design time and, if possible, by using stored procedures. You must explicitly set these commands before calling Update. If you call update but do not have a corresponding command for a specific update (for example, there is no DeleteCommand for the deleted row), an exception is thrown.
The Command parameter can be used to specify input and output values for SQL statements or stored procedures for each modified row in the DataSet. For more information, see Using Parameters with DataAdapter.
If a DataTable is mapped to a single database table or generated from a single database table, you can use the CommandBuilder object to automatically generate DataAdapter DeleteCommand, InsertCommand, and UpdateCommand. For more information, see automatically generated commands.
The Update method resolves the changes back to the data source, but other clients may have modified the data in the data source since the last time the dataset was populated. To refresh the dataset with the current data, use the DataAdapter Fill (fill) dataset again. The new row is added to the table, and the updated information is merged into the existing rows.
To handle exceptions that may occur during the update operation, you can use the RowUpdated event to respond to row update errors when these exceptions occur (see Using the DataAdapter event), or you can DataAdapter.ContinueUpdateOnError set to True, and then respond to an error message stored in the RowError property of a particular row when the Update completes (see Adding and Reading Row error messages).
Note If you call AcceptChanges on a DataSet, DataTable, or DataRow, all Original values for a DataRow will be overwritten by the current value of the DataRow. If you have modified a field value that identifies the row as a unique row, the Original value will no longer match the value in the data source when AcceptChanges is invoked.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.