2. 1. Setting up Objects
Dim objConnection As New OleDb.OleDbConnection( _
Set up the objects
"Provider=Microsoft.Ace.OLEDB.12.0;Data Source
=C:DataBase.accdb")
Dim objTableDA As New _
OleDb.OleDbDataAdapter("Select * from Table", _
objConnection)
Dim objTableCB As New _
OleDb.OleDbCommandBuilder(objTableDA)
Dim objDataSet As New DataSet()
4. 3. Displaying Data on Form
Dim objRow As DataRow
objRow = _ objDataSet.Tables(“Table").Rows.Find _
(Insert Code for Primary Key Here)
lblField1Text = objRow.Item(“Field1")
txtField2.Text = objRow.Item(“Field2")
txtField3.Text = objRow.Item(“Field3")
End Sub
6. 4.Storing Details
Public Sub StoreDetails()
Dim objRow As DataRow
objRow = _
objDataSet.Tables(“Table").Rows.Find(PrimaryKey)
objRow.Item(“Field1") = txtField1.Text
objRow.Item(“Field2") = txtField2.Text
End Sub
7. 5. Update Changes to DataSet
In the previous slide we updated our
database but…
objTableDA.Update(objDataSet, “Table”)
8. 6.Add a New Row to DataSet
Dim objRow As DataRow
objRow = objDataSet.Tables(“Table”).NewRow
objRow.Item(“Field1”) = InputBox(“Field1?”)
objRow.Item(“Field2”) = InputBox(“Field2?”)
objRow.Item(“Field3”) = InputBox(“Field3?”)
objDataSet.Tables(“Table”).Rows.Add(objRow)
objTableDA.Update(objDataSet, “Table”)
9. 7. Deleting a Row
Dim objRow As DataRow
objRow =
objDataSet.Tables(“Tables").Rows.Find(PrimaryKey)
objRow.Delete()
objTableDA.Update(objDataSet, “Table")
Retrieve()
10.
11. 1.Setting up Objects
Dim objConnection As New OleDb.OleDbConnection( _
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source _
=surgery.accdb")
Dim objOwnerDA As New
OleDb.OleDbDataAdapter("Select * from Owners", _
objConnection)
Dim objOwnerCB As New
OleDb.OleDbCommandBuilder(objOwnerDA)
Dim objDS As New DataSet()
12. 2.Fill the Data Set
objDS.Clear()
objOwnerDA.FillSchema(objDS, _
SchemaType.Source, "Owners")
objOwnerDA.Fill(objDataSet, "Owners")
14. Row 0
Row 1
Row 2
Row 3
„objDS.Tables("Owners").Rows.Count = 4
„For i = 0 to 4 would cause a problem
„There is no row 4!
15. 3.Populate ComboBox
cboOwners.Items.Clear()
Dim i As Integer, strCurrentID As String
For i = 0 To objDS.Tables("Owners").Rows.Count - 1
strCurrentID = _
objDS.Tables("Owners").Rows(i).Item("OwnerID")
cboOwners.Items.Add(strCurrentID)
Next
cboOwners.SelectedIndex = 0
16. 4.Display a Record
Public Sub FillOwnersDetails()
Dim objRow As DataRow
objRow = _
objDS.Tables("Owners").Rows.Find _
(cboOwners.SelectedItem.ToString)
lblOwnerID.Text = _
objRow.Item("OwnerID")
txtName.Text = objRow.Item("Name")
txtAddress.Text =
objRow.Item("Address")
End Sub
17. 5.Storing Details
Public Sub StoreOwnerDetails()
Dim objRow As DataRow
If lblOwnerID.Text = "" Then Exit Sub
objRow = _
objDS.Tables("Owners").Rows.Find _
(lblOwnerID.Text)
objRow.Item("Name") = txtName.Text
objRow.Item("Address") = _
txtAddress.Text
End Sub
19. 7.Persisting Changes
Code to be placed behind the save button
If the user has made changes to a field we want
to store the changes to the dataset and then
persist those changes to the DB
1. Call the StoreOwnerDetails()
2. objOwnerDA.Update(objDS, “Owners”)
20. 8.Add Row to our DataSet
Dim objRow As DataRow
objRow = objDS.Tables(“Owners”).NewRow
objRow.Item(“OwnerID”) = InputBox(“Owner ID?”)
objRow.Item(“Name”) = InputBox(“Name?”)
objRow.Item(“Address”) = InputBox(“Address?”)
objDS.Tables(“Owners”).Rows.Add(objRow)
StoreOwnerDetails()
objOwnerDA.Update(objDS, “Owners”)
Remember to loop through records again !
21. 9.Deleting a Row
Dim objRow As DataRow
objRow = _
objDS.Tables("Owners")._
Rows.Find(cboOwners.SelectedItem.ToString)
objRow.Delete()
objOwnerDA.Update(objDS, "Owners")
Retrieve()
22. 10.Creating Relationships
We have created two tables owners and pets.
What if we want to use both tables on our
form?
Pull data from both tables into our dataset
Then set up an OwnerID relationship between
the two.
23.
24. 11.Set up more Objects
Dim objPetDA as New _
OleDbDataAdapter(“Select * from Pets”,_
, objConnection)
Dim objPetCB As New _
OleDbCommandBuilder(objPetDA)
26. Filling in Pet Details
Add the following code to your Retrieve button code or
Retrieve Method
It should go in after you have filled the dataset with
Owners data
objPetDA.FillSchema(objDataSet, _
SchemaType.Source, "Pets")
objPetDA.Fill(objDS, "Pets")
'Setup our Relationship
objDS.Relations.Clear()
objDS.Relations.Add("Owners2Pets", _
objDS.Tables("Owners").Columns("OwnerID"), _
objDS.Tables("Pets").Columns("OwnerID"))
27. FillPetDetails()
Public Sub FillPetDetails()
Dim objOwner As DataRow, objPet As _
DataRow
Dim strPetEntry As String
lstPets.Items.Clear()
objOwner = objDS.Tables(“Owners”)._
Rows.Find(cboOwners.SelectedItem.ToString)
28. Loop through records
For Each objPet in
objOwner.GetChildRows(“Owners2Pets”)
strPetEntry = objItem(“PetID”) & “, “ ” & _
objPet.Item(“PetName”) & “, “ &
objPet.Item(“Type”)
lstPets.Items.Add(strPetEntry)
Next
End Sub
29. Finishing Touches
To end of your retrieve records code add the
following line:
FillPetDetails()
Add the following code to the
SelectedIndexChanged event of our combo
box:
FillPetDetails()
Notas do Editor
Once again we set up a datarow object. The NewRow() method of the dataset will add a new row to our dataset and sets it up as the data row. We can then add values to the fields by using the Item properties of the datarow once we have populated it.Once we are finished adding the Items, we add the row to our dataset. The data has now been added to the “virtual” dataset, we need to tell the dataAdpater to update our database to make the changes to the .accdb file
In order to delete a record we need to tell VB.NET which record to delete. Once again we use the Find method of our dataset in conjunction with a dataRow object.
Instead of using a Popup Box (i.e. inputbox) we could simply have entered in the values via a textbox
'Fill our DataSet with Info from the Pets TableobjPetDA.FillSchema(objDataSet, SchemaType.Source, "Pets")objPetDA.Fill(objDataSet, "Pets")'Setup our RelationshipobjDataSet.Relations.Clear()objDataSet.Relations.Add("Owners2Pets", _objDataSet.Tables("Owners").Columns("OwnerID"), _objDataSet.Tables("Pets").Columns("OwnerID"))