Thông tin tài liệu:
4,2 Thêm và dòng Delete trong một Dataset với ADO.NET Một lần nữa bằng cách sử dụng OleDbDataAdapter và OleDbCommandBuilder đối tượng, điều này thế nào-Để cho bạn thấy làm thế nào để sử dụng điều khiển cởi ra với các tập dữ liệu để thêm và xóa các hàng từ SQL Server.
Nội dung trích xuất từ tài liệu:
Add and Delete Rows in a Dataset with ADO.NET 4.2 Add and Delete Rows in a Dataset with ADO.NETAgain using the OleDbDataAdapter and OleDbCommandBuilder objects, this How-Toshows you how to use unbound controls with the dataset to add and delete rows fromSQL Server.As with editing and updating data, you need to be able to add and delete rows using thedataset. How do you perform this task?TechniqueThe main difference between this technique and the previous one will be which actioncommand you will use with the DataAdapter object. You will also be presented with theAdd method on the Rows collection.StepsOpen and run the VB.NET-Chapter 4 solution. From the main form, click on thecommand button with the caption How-To 4.2. When the form loads, click on the LoadList button to display the customers that begin with the letter A. Click the Add button.You will notice that the fields have now taken on a sunken look and that they are cleared.Fill in the Customer ID and Company Name text boxes with AAA1 and AnotherExample. Now click Save. If you move off the record and move back on, you will noticethat the value has been saved. Now select the new record you added, and click the Deletebutton. The record disappears, and the list box is updated to reflect the deletion. 1. Make a copy of the form you created in the last How-To. 2. Add two buttons for adding and deleting records, setting the properties as listed here in Table 4.3. Table 4.3. Add and Delete Buttons Property Settings Object Property Setting Button Name btnAdd Caption &Add Button Name btnDelete Text &Delete3. Add the following line of code where you placed the other module-level variable declarations. This variable will be set to True in the btnAdd click event, and it can be used when saving the record.4. Dim mblnAdd As Boolean5. Enter the following code to the Click event btnAdd. The first task is to set the mblnAdd variable to True. Then the routine clears the current text in the text boxes. It also enables the text boxes by calling ActiveEditing.6. Private Sub btnAdd_Click(ByVal sender As System.Object,7. ByVal e As System.EventArgs) Handles btnAdd.Click8.9. Dim oCtl As Object10. Dim strName11.12. mblnAdd = True13.14. -- Clear the fields15. For Each oCtl In Me.Controls16.17. If TypeOf oCtl Is TextBox And oCtl.name txtCustLimit Then18.19. strName = Mid(oCtl.Name, 4)20.21. -- By trapping the exception this way, errors are ignored.22. Try23. oCtl.text = 24. Catch oexp As Exception25. End Try26.27. End If28.29. Next30.31. ActivateEditing(True)32.33. End Sub34. Replace the SaveRecord routine with the following code in the form that you created for this How-To. The first change is to add the lines of code that test mblnAdd; if theyre True, call the NewRow method off the datasets Tables collection, specifying the Customers table. The DataRow returned is assigned to mdrCustIndiv. You can then enter the other changes wherever the blnAdd variable is queried.35. Private Sub SaveRecord()36.37. Dim oCtl As Object38. Dim strName As String39.40. If mblnAdd Then41. mdrCustIndiv = mdsCustIndiv.Tables(Customers).NewRow42. End If43.44. -- Start the editing in the datarow.45. mdrCustIndiv.BeginEdit()46.47. -- Run through the text boxes on the form, and48. -- if they match up with a field from the record,49. -- place the value back in the record.50. For Each oCtl In Me.Controls51.52. If TypeOf oCtl Is TextBox Then53.54. strName = Mid(oCtl.Name, 4)55.56. -- By trapping the exception this way, errors are ignored.57. Try58. mdrCustIndiv(strName) = oCtl.text59. Catch oexp As Exception60. End Try61.62. End If63.64. Next65.66. -- Finish the editing of the datarow67. mdrCustIndiv.EndEdit()68.69. Try70.71. If mblnAdd Then72. mdsCustIndiv.Tables(Customers).Rows.Add(mdrCustIndiv)73. End If74.75. -- Create an instance of the command builder76. Dim ocbCustIndiv As OleDb.OleDbCommandBuilder77. ocbCustIndiv = New OleDb.OleDbCommandBuilder(modaCustIndiv)78.79. If mblnAdd Then80. -- Have the command builder create an Insert SQL command81. modaCustIndiv.InsertCommand = ocbCustIndiv.GetInsertCommand82. Else83. -- Have the command builder create an update SQL command84. modaCustIndiv.UpdateCommand = ocbCustIndiv.GetUpdateCommand85. End If86.87. -- Perform the specified SQL command; then close the connection88. modaCustIndiv.Update(mdsCustIndiv, Customers)89. mdsCustIndiv.Tables(Customers).AcceptChanges()90.91. -- Close the connection92. If mblnAdd Then93. modaCustIndiv.InsertCommand.Connection.Close()94. LoadList()95. Else96. modaCustIndiv.UpdateCommand.Connection.Close()97. End If98.99. Catch excData As Exception100. MessageBox.Show(Error Occurred: & excData.Message)101. End Try102.103. End Sub104. Enter the following code to the Click event btnCancel.105. Private Sub btnCancel_Click(ByVal sender As System.Object, _106. ByVal e As System.EventArgs) Handles btnCancel.Click107.108. -- Cancel the current editing.109.110. If mblnAdd Then111. mblnAdd = False112. End If113.114. LoadIndividual()115. ActivateEditing(False)116.117. End Sub ...