Danh mục

Modifying Data Using a Strongly Typed DataSet

Số trang: 3      Loại file: pdf      Dung lượng: 34.79 KB      Lượt xem: 2      Lượt tải: 0    
Thu Hiền

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Modifying Data Using a Strongly Typed DataSet In Chapter 10, you saw how to create and use a strongly typed DataSet class named MyDataSet. You can use objects of this class to represent the Customers table and rows from that table
Nội dung trích xuất từ tài liệu:
Modifying Data Using a Strongly Typed DataSetModifying Data Using a Strongly Typed DataSetIn Chapter 10, you saw how to create and use a strongly typed DataSet class namedMyDataSet. You can use objects of this class to represent the Customers table and rowsfrom that table. In this section, youll see how to modify data using a strongly typedobject of the MyDataSet class.Note One of the features of a strongly typed DataSet object allows you to read a column value using a property with the same name as the column. For example, to read the CustomerID of a column you can use myDataRow.CustomerID rather than myDataRow[CustomerID]. See Chapter 10 for more details on reading column values.The following methods in the MyDataSet class allow you to modify the rows stored in aMyDataSet object: NewCustomersRow(), AddCustomersRow(), andRemoveCustomersRow(). You can find a row using the FindByCustomerID() method.You can check if a column value contains a null value using methods such asIsContactNameNull(), and you can set a column to null using methods such asSetContactNameNull(). Youll see these methods used shortly.Note Youll find a completed VS .NET example project for this section in the StronglyTypedDataSet2 directory. You can open this project in VS .NET by selecting File ➣ Open ➣ Project and opening the WindowsApplication4.csproj file. Youll need to change the ConnectionString property of the sqlConnection1 object to connect to your Northwind database.The Form1_Load() method of the form in the example project shows how to add, modify,and remove a row to a strongly typed DataSet object named myDataSet1. You can see thesteps that accomplish these tasks in the following Form1_Load() method:private void Form1_Load(object sender, System.EventArgs e){ // populate the DataSet with the CustomerID, CompanyName, // and Address columns from the Customers table sqlConnection1.Open(); sqlDataAdapter1.Fill(myDataSet1, Customers); // get the Customers DataTable MyDataSet.CustomersDataTable myDataTable = myDataSet1.Customers;// create a new DataRow in myDataTable using the// NewCustomersRow() method of myDataTableMyDataSet.CustomersRow myDataRow = myDataTable.NewCustomersRow();// set the CustomerID, CompanyName, and Address of myDataRowmyDataRow.CustomerID = J5COM;myDataRow.CompanyName = J5 Company;myDataRow.Address = 1 Main Street;// add the new row to myDataTable using the// AddCustomersRow() methodmyDataTable.AddCustomersRow(myDataRow);// push the new row to the database using// the Update() method of sqlDataAdapter1sqlDataAdapter1.Update(myDataTable);// find the row using the FindByCustomerID()// method of myDataTablemyDataRow = myDataTable.FindByCustomerID(J5COM);// modify the CompanyName and Address of myDataRowmyDataRow.CompanyName = Widgets Inc.;myDataRow.Address = 1 Any Street;// push the modification to the databasesqlDataAdapter1.Update(myDataTable);// display the DataRow objects in myDataTable// in the listView1 objectforeach (MyDataSet.CustomersRow myDataRow2 in myDataTable.Rows){ listView1.Items.Add(myDataRow2.CustomerID); listView1.Items.Add(myDataRow2.CompanyName); // if the Address is null, set Address to Unknown if (myDataRow2.IsAddressNull() == true) { myDataRow2.Address = Unknown; } listView1.Items.Add(myDataRow2.Address);} // find and remove the new row using the // FindByCustomerID() and RemoveCustomersRow() methods // of myDataTable myDataRow = myDataTable.FindByCustomerID(J5COM); myDataTable.RemoveCustomersRow(myDataRow); // push the delete to the database sqlDataAdapter1.Update(myDataTable); sqlConnection1.Close();}Feel free to compile and run the example form.

Tài liệu được xem nhiều: