DEALING WITH MORE THAN ONE TABLERELATIONAL DATABASE
1. Setting up ObjectsDim objConnection As New OleDb.OleDbConnection( _ Set up the objects  "Provider=Microsoft.Ace.OLEDB.1...
2. Filling the DataSetobjDataSet.Clear()objTableDA.FillSchema(objDataSet, _  SchemaType.Source, “Table")objTableDA.Fill(ob...
3. Displaying Data on FormDim objRow As DataRow    objRow = _ objDataSet.Tables(“Table").Rows.Find _  (Insert Code for Pri...
Column 0Row 0Row 1Row 2Row 3
4.Storing DetailsPublic Sub StoreDetails()Dim objRow As DataRow objRow = _ objDataSet.Tables(“Table").Rows.Find(PrimaryKey...
5. Update Changes to DataSet In the previous slide we updated our  database but…objTableDA.Update(objDataSet, “Table”)
6.Add a New Row to DataSetDim objRow As DataRowobjRow = objDataSet.Tables(“Table”).NewRowobjRow.Item(“Field1”) = InputBox(...
7. Deleting a RowDim objRow As DataRow   objRow = objDataSet.Tables(“Tables").Rows.Find(PrimaryKey)  objRow.Delete()  objT...
1.Setting up ObjectsDim objConnection As New OleDb.OleDbConnection( _  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source _  =...
2.Fill the Data SetobjDS.Clear()objOwnerDA.FillSchema(objDS, _  SchemaType.Source, "Owners")objOwnerDA.Fill(objDataSet, "O...
Row 0Row 1Row 2Row 3
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!
3.Populate ComboBoxcboOwners.Items.Clear()Dim i As Integer, strCurrentID As StringFor i = 0 To objDS.Tables("Owners").Rows...
4.Display a RecordPublic Sub FillOwnersDetails()   Dim objRow As DataRow   objRow = _  objDS.Tables("Owners").Rows.Find _ ...
5.Storing DetailsPublic Sub StoreOwnerDetails()Dim objRow As DataRowIf lblOwnerID.Text = "" Then Exit SubobjRow = _objDS.T...
6.ImplementProcedures Call the Fill Owner Details behind the Combo Box
7.Persisting Changes Code to be placed behind the save button If the user has made changes to a field we want  to store ...
8.Add Row to our DataSetDim objRow As DataRowobjRow = objDS.Tables(“Owners”).NewRowobjRow.Item(“OwnerID”) = InputBox(“Owne...
9.Deleting a RowDim objRow As DataRow objRow = _objDS.Tables("Owners")._  Rows.Find(cboOwners.SelectedItem.ToString) objRo...
10.Creating Relationships We have created two tables owners and pets. What if we want to use both tables on our  form? ...
11.Set up more ObjectsDim objPetDA as New _OleDbDataAdapter(“Select * from Pets”,_   , objConnection)Dim objPetCB As New _...
lstPets
Filling in Pet Details   Add the following code to your Retrieve button code or    Retrieve Method   It should go in aft...
FillPetDetails()Public Sub FillPetDetails() Dim objOwner As DataRow, objPet As _ DataRow Dim strPetEntry As String lstPets...
Loop through recordsFor Each objPet in  objOwner.GetChildRows(“Owners2Pets”)  strPetEntry = objItem(“PetID”) & “, “ ” & _ ...
Finishing Touches To end of your retrieve records code add the  following line:      FillPetDetails() Add the following ...
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databases
Upcoming SlideShare
Loading in …5
×

Is2215 lecture8 relational_databases

341 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
341
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 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"))
  • Is2215 lecture8 relational_databases

    1. 1. DEALING WITH MORE THAN ONE TABLERELATIONAL DATABASE
    2. 2. 1. Setting up ObjectsDim 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()
    3. 3. 2. Filling the DataSetobjDataSet.Clear()objTableDA.FillSchema(objDataSet, _ SchemaType.Source, “Table")objTableDA.Fill(objDataSet, “Table")
    4. 4. 3. Displaying Data on FormDim 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
    5. 5. Column 0Row 0Row 1Row 2Row 3
    6. 6. 4.Storing DetailsPublic Sub StoreDetails()Dim objRow As DataRow objRow = _ objDataSet.Tables(“Table").Rows.Find(PrimaryKey) objRow.Item(“Field1") = txtField1.Text objRow.Item(“Field2") = txtField2.TextEnd Sub
    7. 7. 5. Update Changes to DataSet In the previous slide we updated our database but…objTableDA.Update(objDataSet, “Table”)
    8. 8. 6.Add a New Row to DataSetDim objRow As DataRowobjRow = objDataSet.Tables(“Table”).NewRowobjRow.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. 9. 7. Deleting a RowDim objRow As DataRow objRow = objDataSet.Tables(“Tables").Rows.Find(PrimaryKey) objRow.Delete() objTableDA.Update(objDataSet, “Table") Retrieve()
    10. 10. 1.Setting up ObjectsDim 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()
    11. 11. 2.Fill the Data SetobjDS.Clear()objOwnerDA.FillSchema(objDS, _ SchemaType.Source, "Owners")objOwnerDA.Fill(objDataSet, "Owners")
    12. 12. Row 0Row 1Row 2Row 3
    13. 13. 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!
    14. 14. 3.Populate ComboBoxcboOwners.Items.Clear()Dim i As Integer, strCurrentID As StringFor i = 0 To objDS.Tables("Owners").Rows.Count - 1 strCurrentID = _ objDS.Tables("Owners").Rows(i).Item("OwnerID") cboOwners.Items.Add(strCurrentID)NextcboOwners.SelectedIndex = 0
    15. 15. 4.Display a RecordPublic 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
    16. 16. 5.Storing DetailsPublic Sub StoreOwnerDetails()Dim objRow As DataRowIf lblOwnerID.Text = "" Then Exit SubobjRow = _objDS.Tables("Owners").Rows.Find _ (lblOwnerID.Text) objRow.Item("Name") = txtName.Text objRow.Item("Address") = _ txtAddress.TextEnd Sub
    17. 17. 6.ImplementProcedures Call the Fill Owner Details behind the Combo Box
    18. 18. 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 DB1. Call the StoreOwnerDetails()2. objOwnerDA.Update(objDS, “Owners”)
    19. 19. 8.Add Row to our DataSetDim objRow As DataRowobjRow = objDS.Tables(“Owners”).NewRowobjRow.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 !
    20. 20. 9.Deleting a RowDim objRow As DataRow objRow = _objDS.Tables("Owners")._ Rows.Find(cboOwners.SelectedItem.ToString) objRow.Delete() objOwnerDA.Update(objDS, "Owners")Retrieve()
    21. 21. 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.
    22. 22. 11.Set up more ObjectsDim objPetDA as New _OleDbDataAdapter(“Select * from Pets”,_ , objConnection)Dim objPetCB As New _ OleDbCommandBuilder(objPetDA)
    23. 23. lstPets
    24. 24. 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 dataobjPetDA.FillSchema(objDataSet, _SchemaType.Source, "Pets")objPetDA.Fill(objDS, "Pets")Setup our RelationshipobjDS.Relations.Clear()objDS.Relations.Add("Owners2Pets", _objDS.Tables("Owners").Columns("OwnerID"), _objDS.Tables("Pets").Columns("OwnerID"))
    25. 25. 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)
    26. 26. Loop through recordsFor Each objPet in objOwner.GetChildRows(“Owners2Pets”) strPetEntry = objItem(“PetID”) & “, “ ” & _ objPet.Item(“PetName”) & “, “ & objPet.Item(“Type”) lstPets.Items.Add(strPetEntry)NextEnd Sub
    27. 27. 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()

    ×