Fill data in DataSet from multiple tables in VB.NET

To fill a DataSet from multiple database tables, you fill different DataSet objects using different data adapters and call Merge method of DataSet.
  • 14184

You can use Merge method of DataSet to merge other DataSet objects. To fill a DataSet from multiple database tables, you fill different DataSet objects using different data adapters and call Merge method of DataSet.

The following source code reads data from three database tables - Heading, SubHead, and Link, merge all data sets and view data in a DataGrid control.

Private connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=F:\C#\Magazine\Extranet.mdb"
Private conn As OleDbConnection = New OleDbConnection(connStr)
conn.Open()
Dim str As String = "SELECT * FROM Heading"
Dim da1 As OleDbDataAdapter = New OleDbDataAdapter(str, conn)
Dim ds1 As DataSet = New DataSet("Heading")
da1.Fill(ds1, "Heading")
Dim ds2 As DataSet = New DataSet("SubHead")
str = "SELECT * FROM SubHeading"
Dim da2 As OleDbDataAdapter = New OleDbDataAdapter(str, conn)
da2.Fill(ds2, "SubHeading")
Dim ds3 As DataSet = New DataSet("Link")
str = "SELECT * FROM Link"
Dim da3 As OleDbDataAdapter = New OleDbDataAdapter(str, conn)
da3.Fill(ds3, "Link")
ds1.Merge(ds2)
ds1.Merge(ds3)
dataGrid1.DataSource = ds1.DefaultViewManager
conn.Close()

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.