SlideShare uma empresa Scribd logo
1 de 30
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()
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.
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

Mais conteúdo relacionado

Mais procurados

The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210Mahmoud Samir Fayed
 
Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query OptimizationMongoDB
 
Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed versionBruce McPherson
 
The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210Mahmoud Samir Fayed
 
JavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerJavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerBruce McPherson
 
Ruby on Rails Developer - Allerin
Ruby on Rails Developer - AllerinRuby on Rails Developer - Allerin
Ruby on Rails Developer - AllerinLauree R
 
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31Mahmoud Samir Fayed
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventasDAYANA RETO
 
Visual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerVisual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerDarwin Durand
 
The Ring programming language version 1.7 book - Part 41 of 196
The Ring programming language version 1.7 book - Part 41 of 196The Ring programming language version 1.7 book - Part 41 of 196
The Ring programming language version 1.7 book - Part 41 of 196Mahmoud Samir Fayed
 
Taming the beast - how to tame React & GraphQL, one error at a time
Taming the beast - how to tame React & GraphQL, one error at a timeTaming the beast - how to tame React & GraphQL, one error at a time
Taming the beast - how to tame React & GraphQL, one error at a timeSusanna Wong
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive ShellGiovanni Lodi
 
Simple.Data intro slides
Simple.Data intro slidesSimple.Data intro slides
Simple.Data intro slidesMark Rendle
 
Indexing and Query Optimization
Indexing and Query OptimizationIndexing and Query Optimization
Indexing and Query OptimizationMongoDB
 
SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)Darwin Durand
 

Mais procurados (20)

The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31
 
Excel Scripting
Excel Scripting Excel Scripting
Excel Scripting
 
The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210
 
Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query Optimization
 
Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed version
 
The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210
 
JavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerJavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primer
 
Ruby on Rails Developer - Allerin
Ruby on Rails Developer - AllerinRuby on Rails Developer - Allerin
Ruby on Rails Developer - Allerin
 
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventas
 
Visual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerVisual Studio.Net - Sql Server
Visual Studio.Net - Sql Server
 
Dbabstraction
DbabstractionDbabstraction
Dbabstraction
 
The Ring programming language version 1.7 book - Part 41 of 196
The Ring programming language version 1.7 book - Part 41 of 196The Ring programming language version 1.7 book - Part 41 of 196
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
Taming the beast - how to tame React & GraphQL, one error at a timeTaming the beast - how to tame React & GraphQL, one error at a time
Taming the beast - how to tame React & GraphQL, one error at a time
 
Js objects
Js objectsJs objects
Js objects
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
 
Simple.Data intro slides
Simple.Data intro slidesSimple.Data intro slides
Simple.Data intro slides
 
Indexing and Query Optimization
Indexing and Query OptimizationIndexing and Query Optimization
Indexing and Query Optimization
 
Mongo db for C# Developers
Mongo db for C# DevelopersMongo db for C# Developers
Mongo db for C# Developers
 
SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)
 

Semelhante a Is2215 lecture8 relational_databases

The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189Mahmoud Samir Fayed
 
Introduction tomongodb
Introduction tomongodbIntroduction tomongodb
Introduction tomongodbLee Theobald
 
Mongo Nosql CRUD Operations
Mongo Nosql CRUD OperationsMongo Nosql CRUD Operations
Mongo Nosql CRUD Operationsanujaggarwal49
 
Adodb Scripts And Some Sample Scripts[1]
Adodb Scripts And Some Sample Scripts[1]Adodb Scripts And Some Sample Scripts[1]
Adodb Scripts And Some Sample Scripts[1]testduser1
 
Adodb Scripts And Some Sample Scripts[1]
Adodb Scripts And Some Sample Scripts[1]Adodb Scripts And Some Sample Scripts[1]
Adodb Scripts And Some Sample Scripts[1]User1test
 
The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 32 of 196
The Ring programming language version 1.7 book - Part 32 of 196The Ring programming language version 1.7 book - Part 32 of 196
The Ring programming language version 1.7 book - Part 32 of 196Mahmoud Samir Fayed
 
E2D3 ver. 0.2 API Instruction
E2D3 ver. 0.2 API InstructionE2D3 ver. 0.2 API Instruction
E2D3 ver. 0.2 API InstructionE2D3.org
 
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptx171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptxsukrithlal008
 
The Ring programming language version 1.8 book - Part 43 of 202
The Ring programming language version 1.8 book - Part 43 of 202The Ring programming language version 1.8 book - Part 43 of 202
The Ring programming language version 1.8 book - Part 43 of 202Mahmoud Samir Fayed
 
Disconnected Architecture and Crystal report in VB.NET
Disconnected Architecture and Crystal report in VB.NETDisconnected Architecture and Crystal report in VB.NET
Disconnected Architecture and Crystal report in VB.NETEverywhere
 

Semelhante a Is2215 lecture8 relational_databases (20)

Public class form1
Public class form1Public class form1
Public class form1
 
Public class form1
Public class form1Public class form1
Public class form1
 
Python my SQL - create table
Python my SQL - create tablePython my SQL - create table
Python my SQL - create table
 
The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189
 
Mongo db basics
Mongo db basicsMongo db basics
Mongo db basics
 
Experiment no 2
Experiment no 2Experiment no 2
Experiment no 2
 
Introduction tomongodb
Introduction tomongodbIntroduction tomongodb
Introduction tomongodb
 
QTP
QTPQTP
QTP
 
Python database access
Python database accessPython database access
Python database access
 
Mongo Nosql CRUD Operations
Mongo Nosql CRUD OperationsMongo Nosql CRUD Operations
Mongo Nosql CRUD Operations
 
2310 b 09
2310 b 092310 b 09
2310 b 09
 
Adodb Scripts And Some Sample Scripts[1]
Adodb Scripts And Some Sample Scripts[1]Adodb Scripts And Some Sample Scripts[1]
Adodb Scripts And Some Sample Scripts[1]
 
Adodb Scripts And Some Sample Scripts[1]
Adodb Scripts And Some Sample Scripts[1]Adodb Scripts And Some Sample Scripts[1]
Adodb Scripts And Some Sample Scripts[1]
 
The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202
 
The Ring programming language version 1.7 book - Part 32 of 196
The Ring programming language version 1.7 book - Part 32 of 196The Ring programming language version 1.7 book - Part 32 of 196
The Ring programming language version 1.7 book - Part 32 of 196
 
Ado.Net
Ado.NetAdo.Net
Ado.Net
 
E2D3 ver. 0.2 API Instruction
E2D3 ver. 0.2 API InstructionE2D3 ver. 0.2 API Instruction
E2D3 ver. 0.2 API Instruction
 
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptx171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
 
The Ring programming language version 1.8 book - Part 43 of 202
The Ring programming language version 1.8 book - Part 43 of 202The Ring programming language version 1.8 book - Part 43 of 202
The Ring programming language version 1.8 book - Part 43 of 202
 
Disconnected Architecture and Crystal report in VB.NET
Disconnected Architecture and Crystal report in VB.NETDisconnected Architecture and Crystal report in VB.NET
Disconnected Architecture and Crystal report in VB.NET
 

Mais de dannygriff1

Stocks&bonds2214 1
Stocks&bonds2214 1Stocks&bonds2214 1
Stocks&bonds2214 1dannygriff1
 
Profitability&npv
Profitability&npvProfitability&npv
Profitability&npvdannygriff1
 
Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)dannygriff1
 
Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)dannygriff1
 
Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)dannygriff1
 
Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)dannygriff1
 
Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)dannygriff1
 
6 price and output determination- monopoly
6 price and output determination- monopoly6 price and output determination- monopoly
6 price and output determination- monopolydannygriff1
 
5 industry structure and competition analysis
5  industry structure and competition analysis5  industry structure and competition analysis
5 industry structure and competition analysisdannygriff1
 
4 production and cost
4  production and cost4  production and cost
4 production and costdannygriff1
 
3 consumer choice
3 consumer choice3 consumer choice
3 consumer choicedannygriff1
 
2 demand-supply and elasticity
2  demand-supply and elasticity2  demand-supply and elasticity
2 demand-supply and elasticitydannygriff1
 
1 goals of the firm
1  goals of the firm1  goals of the firm
1 goals of the firmdannygriff1
 
Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)dannygriff1
 

Mais de dannygriff1 (20)

Stocks&bonds2214 1
Stocks&bonds2214 1Stocks&bonds2214 1
Stocks&bonds2214 1
 
Risk08a
Risk08aRisk08a
Risk08a
 
Profitability&npv
Profitability&npvProfitability&npv
Profitability&npv
 
Npvrisk
NpvriskNpvrisk
Npvrisk
 
Npv2214(1)
Npv2214(1)Npv2214(1)
Npv2214(1)
 
Irr(1)
Irr(1)Irr(1)
Irr(1)
 
Npv rule
Npv ruleNpv rule
Npv rule
 
Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)
 
Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)
 
Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)
 
Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)
 
Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)
 
6 price and output determination- monopoly
6 price and output determination- monopoly6 price and output determination- monopoly
6 price and output determination- monopoly
 
5 industry structure and competition analysis
5  industry structure and competition analysis5  industry structure and competition analysis
5 industry structure and competition analysis
 
4 production and cost
4  production and cost4  production and cost
4 production and cost
 
3 consumer choice
3 consumer choice3 consumer choice
3 consumer choice
 
2 demand-supply and elasticity
2  demand-supply and elasticity2  demand-supply and elasticity
2 demand-supply and elasticity
 
1 goals of the firm
1  goals of the firm1  goals of the firm
1 goals of the firm
 
Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)
 
Mcq sample
Mcq sampleMcq sample
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()
  • 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")
  • 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.
  • 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

  1. 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
  2. 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.
  3. Instead of using a Popup Box (i.e. inputbox) we could simply have entered in the values via a textbox
  4. '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"))