Download Updating Database

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Jet Database Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
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.
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.
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)
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.
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.
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.
DataTable’s Select Method
• Select(FilterExpression)
– Gets an array of all System.Data.DataRow
objects that match the filter criteria
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")
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.
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")
Modification Example 2: Use Table’s
Rows Property
Example 2: Use Table’s Rows Property
Form 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
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
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
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))
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.
Deletion
• The dataRow’s Delete method changes the
RowState to Deleted.
• Apply the deletions to the database.
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")
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