1 / 20

Updating Database

Updating Database. Two Approaches to Update Database. 1. Using the DataSet object, the updates are first applied to the DataSet then applied to the database using the Adapter object. 2. Using Command object’s ExecuteNonQuery to run the SQL statements directly on the database.

jonesdebra
Download Presentation

Updating Database

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Updating Database

  2. Two Approaches to Update Database • 1. Using the DataSet object, the updates are first applied to the DataSet then applied to the database using the Adapter object. • 2. Using Command object’s ExecuteNonQuery to run the SQL statements directly on the database.

  3. Adding a New Record to a DataTable • A DataTable consists of many rows. Each of these rows is represented by a DataRow object, and is held within the DataTable Rows property. • To add a new row: • Define a DataRow object. • Use DataTable’s NewRow() method to create the new row. • Assign value to fields in the new row. • Use the ADD method of the Rows to add the new row to the DataTable.

  4. Code Example dim objNewRow as DataRow objTable=objDataSet.Tables("Cust") objNewRow = objTable.NewRow() objNewRow("cid")=cid.text objNewRow("Cname")=cname.text objNewRow("city")=city.text objNewRow("rating")=rating.text objTable.Rows.Add(objNewRow)

  5. Add the New Row to the Data Source • The Adapter object provides two-way interaction between the database and the DataSet. • It has SelectCommand, InsertCommand, DeleteCommand, UpdateCommand properties that hold command objects to run SQL statements. • To initialize these properties, we can: • define a command object( typically for SelectCommand) • use a CommandBuilder object (for other commands) • The Update method of an Adapter object executes commands to update the database.

  6. OleDBCommandBuilder • Property: • Adapter • Methods: • GetDeleteCommand • GetInsertCommand • GetUpdateCommand • The commandBuilder uses the Adapter’s SelectCommand to generate Insert, Delete, and Update commands. • Adapter’s InsertCommand, DeleteCommand, UpdateCommand properties generated by the Command Builder object can transfer multiple updates back to the database.

  7. Example Using the CommandBuilder dim objBlder=new OleDBCommandBuilder objBlder=new OleDBCommandBuilder(objAdapter) objAdapter.InsertCommand=objBlder.GetInsertCommand() objAdapter.Update(objDataset,"cust") Note: The commandBuilder uses the Adapter’s SelectCommand to generate the Insert command.

  8. DataTable’s Select Method • Select(FilterExpression) • Gets an array of all System.Data.DataRow objects that match the filter criteria

  9. Select Method Example dim objRows() as DataRow objRows=objTable.select("cid= '" & CidList.selectedItem & "'") cid.text=objRows(0).item("cid") cname.text=objRows(0).item("cname") city.text=objRows(0).item("city") rating.text=objRows(0).item("rating")

  10. Modifying An Existing Record • Use Table’s Select method to select the records for change. • Assign new values to selected records. • Apply the update to the database.

  11. Modification Example objTable=objDataset.tables("cust") objRows=objTable.select("cname= '" & CnameList.selectedItem.text & "'") objRows(0).item("cid")=cid.text objRows(0).item("cname")=cname.text objRows(0).item("city")=city.text objRows(0).item("rating")=rating.text strSQL = "select * from customer;" dim objAdapter as new OledbDataAdapter(strSQL, objConn) dim objBlder=new OleDBCommandBuilder objBlder=new OleDBCommandBuilder(objAdapter) objAdapter.UpdateCommand=objBlder.GetUpdateCommand() objAdapter.Update(objDataset,"cust")

  12. Modification Example 2: Use Table’s Rows Property

  13. Example 2: Use Table’s Rows PropertyForm Load and Next Button Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load cid.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("cid") cname.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("cname") city.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("city") rating.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("rating") End Sub Private Sub NextButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click objDataSet.Tables("customer").Rows(rowIndex).Item("cid") = cid.Text objDataSet.Tables("customer").Rows(rowIndex).Item("cname") = cname.Text objDataSet.Tables("customer").Rows(rowIndex).Item("city") = city.Text objDataSet.Tables("customer").Rows(rowIndex).Item("rating") = rating.Text rowIndex += 1 cid.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("cid") cname.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("cname") city.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("city") rating.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("rating") End Sub

  14. Update Button Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLCust As String = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQLCust, objConn) Dim objBlder = New OleDbCommandBuilder() objBlder = New OleDbCommandBuilder(objAdapter) objAdapter.UpdateCommand = objBlder.GetUpdateCommand() objAdapter.Update(objDataSet, "CUSTOMER") End Sub

  15. How .NET Keeps Track of Updates in the DataSet • Each column in every row of a table maintains DataRowVersion: • Original: The value in the column when the DataTable was created and filled with data. • Current: The actual column value after changes have been made. • Each row in a table maintains RowState: • Unchanged • Added • Modified • Deleted

  16. DataRowVersion Demo • objTable.Rows(0)("rating")="E" • Messagebox.Show("Original rating is: " + objTable.Rows(0)("rating", DataRowVersion.Original)) • Messagebox.Show("New rating is: " + objTable.Rows(0)("rating", DataRowVersion.Current))

  17. RowState Example • DataSet object’s GetChanges method returns a DataSet that contains updated rows. • objModifiedDS=objDataSet.GetChanges(DataRowState.Modified) • objAddedDS= objDataSet.GetChanges(DataRowState.Added) • objDeletedDS= objDataSet.GetChanges(DataRowState.Deleted) • DataTable object’s GetChanges method returns a DataTable that contains updated tows.

  18. Deletion • The dataRow’s Delete method changes the RowState to Deleted. • Apply the deletions to the database.

  19. Delete Example objTable=objDataset.tables("cust") objRows=objTable.select("cname= '" & CnameList.selectedItem.text & "'") objRows(0).Delete() strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) strSQL = "select * from customer;" dim objAdapter as new OledbDataAdapter(strSQL, objConn) dim objBlder=new OleDBCommandBuilder objBlder=new OleDBCommandBuilder(objAdapter) objAdapter.DeleteCommand=objBlder.GetDeleteCommand() objAdapter.Update(objDataset,"cust")

  20. Use Table’s Rows with Index Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click objDataSet.Tables("customer").Rows(rowIndex).Delete() rowIndex += 1 End Sub

More Related