tailieunhanh - Modifying Rows in a DataTable phần 1

Modifying Rows in a DataTable In this section, you'll see the steps required to add, modify, and remove DataRow objects from a DataTable and then push those changes to the database | Modifying Rows in a DataTable In this section you ll see the steps required to add modify and remove DataRow objects from a DataTable and then push those changes to the database. The examples in this section show how to add modify and delete rows in the Customers database table. Note You ll find a complete program named in the ch11 directory that illustrates the use of the methods shown in this section. This program listing is omitted from this book for brevity. Setting up a DataAdapter to Push Changes to the Database In Chapter 10 you saw that before you call the Fill method of your DataAdapter to read rows from the database you first need to set the SelectCommand property of your DataAdapter. For example SqlCommand mySelectCommand SELECT CustomerID CompanyName Address FROM Customers ORDER BY CustomerID SqlDataAdapter mySqlDataAdapter new SqlDataAdapter mySelectCommand The SELECT statement is then run when you call the mySqlDataAdapter object s Fill method to retrieve rows from the Customers table into a DataSet. Similarly before you can push changes to the database you must first set up your DataAdapter with Command objects containing appropriate SQL INSERT UPDATE and DELETE statements. You store these Command objects in your DataAdapter object s InsertCommand UpdateCommand and DeleteCommand properties. You push changes from your DataSet to the database using the Update method of your DataAdapter. When you add modify or remove DataRow objects from your DataSet and then call the Update method of your DataAdapter the appropriate InsertCommand UpdateCommand or DeleteCommand is run to push your changes to the database. Let s take a look at how to set the InsertCommand UpdateCommand and DeleteCommand properties of a DataAdapter. Setting the InsertCommand Property of a DataAdapter The following example creates a SqlCommand object named myInsertCommand

TÀI LIỆU LIÊN QUAN
10    158    1
6    184    1
7    162    1
5    157    1
6    160    1
6    152    1
6    150    1
6    206    1
7    154    1
5    121    1