tailieunhanh - Microsoft SQL Server 2005 Developer’s Guide- P12

Microsoft SQL Server 2005 Developer’s Guide- P12:This book is the successor to the SQL Server 2000 Developer’s Guide, which was extremely successful thanks to all of the supportive SQL Server developers who bought that edition of the book. Our first thanks go to all of the people who encouraged us to write another book about Microsoft’s incredible new relational database server: SQL Server 2005. | Chapter 6 Developing Database Applications with 219 Here you can see again how the connection object has been passed in at the top of the routine. DataAdapter DataSet and CommandBuilder objects are then created. The DataSet is then filled inside the Try-Catch loop. The next statement shows retrieving the last row in the SpecialOffer table into a DataRow object. The Description field of the DataRow is then set with a new value which changes the property for this row to reflect Modified. The next statement calls the DataAdapter s Update method. The Update method determines the appropriate command to execute from the value of the RowState property in this case it will call the UpdateCommand of the DataAdapter to resolve the changed row back to the data source. Delete Using the CommandBuilder The next example shows deleting a record from the database. Private Sub DataSetDeleteSql cn As SqlConnection Create the dataadapter and commandbuilder Dim sqlDA As SqlDataAdapter New SqlDataAdapter _ SELECT FROM cn Dim ds New DataSet Dim sqlCB New SqlCommandBuilder sqlDA Try Populate the dataset ds SpecialOffer Mark the record in the datatable for deletion Dim sqlDR SpecialOffer .Rows _ SpecialOffer . - 1 Delete the record from the database table ds SpecialOffer Catch e As Exception MsgBox End Try End Sub Again you can see the connection object passed into the routine and the DataAdapter DataSet and CommandBuilder objects being created. Then the DataSet is filled in the Try-Catch loop. The next statement retrieves the last row from the SpecialOffer DataTable into a DataRow object. Then the DataRow s Delete method is called to delete the row from the DataTable SpecialOffer. In reality this does not physically delete the row from the DataTable but instead sets the property to Deleted. Next when the DataAdapter s 220 Microsoft SQL Server 2005