Adding Update Functionality//增加修改功能

You've seen how your GridView control lost some functionality //功能when using an object data source //对像型数据源that only provided a method for data retrieval//数据恢复. You will now see how to add methods to update//修改 the database encapsulated //嵌入,引入by the object data source. This will, in turn//依次,接下来;反之,反过来, enable you to add this functionality //功能to your GridView control, all with only a small amount of code.

1. Select the App_Code/DataAccess.vb (or .cs) tab. To the DataAccess class add methods for updating records in the database, removing the line breaks required for Web presentation.

C# VB  
public void UpdateAuthor(string au_lname, string au_fname, string phone, string address, string city,
            string state, string zip, bool contract, string Original_au_id)
            {
            AuthorsDataSetTableAdapters.authorsTableAdapter authorsTableAdapter =
            new AuthorsDataSetTableAdapters.authorsTableAdapter();
            authorsTableAdapter.Update(Original_au_id, au_lname, au_fname, phone, address,
            city, state, zip, contract, Original_au_id);
            }
            

Notice that there are only two lines of code. You are taking advantage of //利用the new TableAdapter class's inherent //内在的,固有的support for updating a database. It was able to generate an SQL UPDATE statement //语句automatically based on your SQL SELECT statement. The data access class merely//仅仅 wraps //包,裹the existing TableAdapter logic, passing in the required arguments.<注:这句话没有读懂>

When you make changes to the data access layer you must update //修改the ObjectDataSource configuration.

2.  Click the Authors.aspx tab. In the Properties window, for the ObjectDataSource, set OldValuesParameterFormatString=Original_{0}. Next, open the ObjectDataSource Tasks menu. Click Configure Data Source and then click Next. In the Define Data Methods step, click the Update tab. For Choose a method select UpdateAuthor. Click Next and then click Finish.

3. Open the GridView Tasks menu. Check Enable Editing, which is now a new option with your updated //修改后的ObjectDataSource control.

4. Press ESC to close the GridView Tasks menu and then press F5 to run the application.

5. When the page loads click Edit for any row and modify one of the row values. Click Update to push the changes back to the database.

6. Close the browser.

posted on 2007-04-11 11:08  改变热爱  阅读(211)  评论(0)    收藏  举报

导航