SlideShare a Scribd company logo
DEALING WITH MORE THAN ONE TABLE

RELATIONAL DATABASE
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()
2. Filling the DataSet

objDataSet.Clear()

objTableDA.FillSchema(objDataSet, _
  SchemaType.Source, “Table")
objTableDA.Fill(objDataSet, “Table")
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
Column 0




Row 0
Row 1
Row 2
Row 3
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
5. Update Changes to DataSet

 In the previous slide we updated our
  database but…

objTableDA.Update(objDataSet, “Table”)
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”)
7. Deleting a Row

Dim objRow As DataRow
   objRow =
 objDataSet.Tables(“Tables").Rows.Find(PrimaryKey)
  objRow.Delete()
  objTableDA.Update(objDataSet, “Table")
 Retrieve()
Is2215 lecture8 relational_databases
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()
2.Fill the Data Set

objDS.Clear()

objOwnerDA.FillSchema(objDS, _
  SchemaType.Source, "Owners")
objOwnerDA.Fill(objDataSet, "Owners")
Row 0
Row 1
Row 2
Row 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 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
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
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
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 the changes to the dataset and then
  persist those changes to the DB

1. Call the StoreOwnerDetails()
2. objOwnerDA.Update(objDS, “Owners”)
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 !
9.Deleting a Row

Dim objRow As DataRow
 objRow = _
objDS.Tables("Owners")._
  Rows.Find(cboOwners.SelectedItem.ToString)
 objRow.Delete()
 objOwnerDA.Update(objDS, "Owners")
Retrieve()
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.
Is2215 lecture8 relational_databases
11.Set up more Objects

Dim objPetDA as New _
OleDbDataAdapter(“Select * from Pets”,_
   , objConnection)
Dim objPetCB As New _
  OleDbCommandBuilder(objPetDA)
lstPets
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"))
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)
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
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()
Is2215 lecture8 relational_databases

More Related Content

DOCX
โค้ด
PDF
Assist9 bmis
DOCX
Accessing data with android cursors
PPT
قائمة الدول
PPT
قائمة القارات
DOCX
Blank Row Find and Select
PDF
The Ring programming language version 1.7 book - Part 48 of 196
PDF
A evolução da persistência de dados (com sqlite) no android
โค้ด
Assist9 bmis
Accessing data with android cursors
قائمة الدول
قائمة القارات
Blank Row Find and Select
The Ring programming language version 1.7 book - Part 48 of 196
A evolução da persistência de dados (com sqlite) no android

What's hot (20)

PDF
The Ring programming language version 1.5 book - Part 8 of 31
DOC
Excel Scripting
PDF
The Ring programming language version 1.9 book - Part 46 of 210
PPTX
Indexing & Query Optimization
PPTX
Google apps script database abstraction exposed version
PDF
The Ring programming language version 1.9 book - Part 53 of 210
PDF
JavaScript client API for Google Apps Script API primer
DOCX
Ruby on Rails Developer - Allerin
PDF
The Ring programming language version 1.4.1 book - Part 13 of 31
PDF
Sistema de ventas
PDF
Visual Studio.Net - Sql Server
PPTX
Dbabstraction
PDF
The Ring programming language version 1.7 book - Part 41 of 196
PDF
Taming the beast - how to tame React & GraphQL, one error at a time
PDF
Js objects
PDF
Functional Core, Reactive Shell
PPTX
Simple.Data intro slides
PPTX
Indexing and Query Optimization
PDF
Mongo db for C# Developers
PDF
SISTEMA DE FACTURACION (Ejemplo desarrollado)
The Ring programming language version 1.5 book - Part 8 of 31
Excel Scripting
The Ring programming language version 1.9 book - Part 46 of 210
Indexing & Query Optimization
Google apps script database abstraction exposed version
The Ring programming language version 1.9 book - Part 53 of 210
JavaScript client API for Google Apps Script API primer
Ruby on Rails Developer - Allerin
The Ring programming language version 1.4.1 book - Part 13 of 31
Sistema de ventas
Visual Studio.Net - Sql Server
Dbabstraction
The Ring programming language version 1.7 book - Part 41 of 196
Taming the beast - how to tame React & GraphQL, one error at a time
Js objects
Functional Core, Reactive Shell
Simple.Data intro slides
Indexing and Query Optimization
Mongo db for C# Developers
SISTEMA DE FACTURACION (Ejemplo desarrollado)
Ad

Similar to Is2215 lecture8 relational_databases (20)

PPTX
76.pptx ajx ppt file for univercity of granted
PPS
VISUAL BASIC .net data accesss vii
PDF
Inventory management
PPT
Advanced dot net
PPT
Dat402
PPT
2310 b 10
PDF
ALL NEW OOP 2014
PPTX
Application of Insert and select notes.ppt x
PPTX
Ado.net with asp.net
PDF
Form1.vb
DOCX
Inventory program in mca p1
PPTX
ADO.NET -database connection
PPTX
Access ppt
PPTX
Operate Spreadsheet applications ppt.pptx
PPT
Database Connection
PPT
Χρήση Vba για την πρόσβαση σε βάση δεδομένων
PPTX
MS Access teaching powerpoint tasks
PPTX
Module 08 Access & Use Database Application.pptx
76.pptx ajx ppt file for univercity of granted
VISUAL BASIC .net data accesss vii
Inventory management
Advanced dot net
Dat402
2310 b 10
ALL NEW OOP 2014
Application of Insert and select notes.ppt x
Ado.net with asp.net
Form1.vb
Inventory program in mca p1
ADO.NET -database connection
Access ppt
Operate Spreadsheet applications ppt.pptx
Database Connection
Χρήση Vba για την πρόσβαση σε βάση δεδομένων
MS Access teaching powerpoint tasks
Module 08 Access & Use Database Application.pptx
Ad

More from dannygriff1 (20)

PPT
Stocks&bonds2214 1
PPT
Risk08a
PPT
Profitability&npv
PPT
Npvrisk
PPT
Npv2214(1)
PPT
Irr(1)
PPT
Npv rule
PPTX
Ec2204 tutorial 8(2)
PPTX
Ec2204 tutorial 4(1)
PPTX
Ec2204 tutorial 3(1)
PPTX
Ec2204 tutorial 2(2)
PPTX
Ec2204 tutorial 1(2)
PPT
6 price and output determination- monopoly
PPT
5 industry structure and competition analysis
PPT
4 production and cost
PPT
3 consumer choice
PPT
2 demand-supply and elasticity
PPT
1 goals of the firm
PPTX
Ec2204 tutorial 6(1)
PPTX
Mcq sample
Stocks&bonds2214 1
Risk08a
Profitability&npv
Npvrisk
Npv2214(1)
Irr(1)
Npv rule
Ec2204 tutorial 8(2)
Ec2204 tutorial 4(1)
Ec2204 tutorial 3(1)
Ec2204 tutorial 2(2)
Ec2204 tutorial 1(2)
6 price and output determination- monopoly
5 industry structure and competition analysis
4 production and cost
3 consumer choice
2 demand-supply and elasticity
1 goals of the firm
Ec2204 tutorial 6(1)
Mcq sample

Is2215 lecture8 relational_databases

  • 1. DEALING WITH MORE THAN ONE TABLE RELATIONAL DATABASE
  • 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()
  • 3. 2. Filling the DataSet objDataSet.Clear() objTableDA.FillSchema(objDataSet, _ SchemaType.Source, “Table") objTableDA.Fill(objDataSet, “Table")
  • 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
  • 5. Column 0 Row 0 Row 1 Row 2 Row 3
  • 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()
  • 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")
  • 13. Row 0 Row 1 Row 2 Row 3
  • 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
  • 18. 6.ImplementProcedures  Call the Fill Owner Details behind the Combo Box
  • 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.
  • 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()

Editor's Notes

  • #9: 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
  • #10: 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.
  • #21: Instead of using a Popup Box (i.e. inputbox) we could simply have entered in the values via a textbox
  • #27: '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"))