Ado.Net Getting Started (5)

zhaozj2021-02-16  54

5. Update data

The web program typically updates the data using a non-manifest statement or by parameter processing. However, when you meet the downtown data, you may want to use the built-in service to update all the required revisions. To complete this work. ADO Batch update mechanism.

The UpdateBatch method is used to send the RECORDSET change stored in the copy buffer to the server to update the data source. It uses an open lock that allows all pending local changes. It also transmits all changes to the data source in a single operation. Open lock occurs only when the data source locks to change after the submission. Open lock allows two users to access the same record at the same time, but the changes entered by a user will soon be overwritten by another user. Of course, this approach requires data sources to detect and prevent data conflicts. The entire data source is also required to be more stable and there is no frequent changes. Otherwise, it is not difficult to imagine that coordination cost will soon exceed the savings caused by the replacement strict lock. In fact, use the UpdateBatch method to return an error when any changes fails. You can then access the error through the ErrorS collection and an Error object.

It is important to understand why the ADO.NET model is a more powerful tool for updating data, understanding the working principle of open-locked in Ado is critical. In the ADO code, you can't control everything that happened after calling UpdateBatch. That is, the update is performed on the server by scrolling the changed row, and then compares the current value in the corresponding record in the original value, and the data source. When all values ​​are consistent, the appropriate SQL statement (INSERT, UPDATE or DELETE) is performed on the table.

The above statement shows that you cannot control the SQL statement. The update code located in the server is neither better than yourself, nor does it work in the case of the non-SQL provider you use. At the beginning of this chapter, I have already told the web application is a typical process for updating the data by parameterizing the storage process. Anyway, if you use a batch update, the situation will be different.

In ADO.NET, the model has been expanded. Now it uses a more generic architecture, through which you can specify your own command statement, such as insert, delete, update, and select. More obvious, You can observe the attempt to extract data from the data source, and regardless of the nature of the data source, you can provide the same support. ADO.NET's batch update, ask you to create a DataSetCommand object: SqlDataSetCommand or AdodatasetCommand

Note: In Beta 2, DataSetCommand objects are called DataAdapter objects.

Once you have a DataSetCommand object, you can use its Update method. DataSetCommand provides a range of properties: such as InsertCommand, DeleteCommand, UpdateCommand, And SELECTCOMMAND. They are all Command objects, but you can't set them unless default The settings are not completed in your request. This is the same as the ADO. During the Update process, if the xxxCommand property is set, the primary keyword already exists, the Command object is automatically generated.

The following code shows how to set the primary key for EmployeesList Table,

Datacolumn [] keys = new datacolumn [1];

Keys [0] = m_ods.tables ["employeeslist"]. Columns ["employeeid"];

m_ods.tables ["employeeslist"]. primarykey = keys;

The primary key is basically a array of Datacolumn objects.

If you want to use the stored procedure to update your form, or you will use a dedicated non-SQL data provider to operate, you will often use these command properties.

转载请注明原文地址:https://www.9cbs.com/read-23555.html

New Post(0)