Free Training
C Language   |   CSS   |   MainFrame   |   VBScript   |   PHP   |   XML   |   C++ Tutorials   |   Ajax   |   JavaScript   |   CSS3   |   UML   |   jQuery   |   Microsoft AJAX

Sponsored Links

ODP.NET Tutorials

 
Home Tutorials ODP.NET
 

ODP.NET - Populating a Dataset with Multiple Data Tables

 

ODP.NET - Populating a Dataset with Multiple Data Tables

Now, let us add more than one data table into a dataset The following code retrieves a list of department details into a data table named Departments and another list of employee details into a data table named Employees:



Sample Code
  1. Imports Oracle.DataAccess.Client
  2. Public Class Form7
  3.  
  4.   Private Sub btnData_Click(ByVal sender As 
  5.   System.ObjectByVal e As System.EventArgs) Handles 
  6.   btnData.Click
  7.     'create connection to db
  8.     Dim cn As New OracleConnection("Data Source=xe; _
  9.                           User Id=scott;Password=tiger")
  10.     Try
  11.       Dim ds As New DataSet
  12.       Dim adp As OracleDataAdapter
  13.  
  14.       adp = New OracleDataAdapter("SELECT deptno, 
  15.                             dname, loc FROM Dept", cn)
  16.       adp.Fill(ds, "Departments")
  17.  
  18.       adp.Dispose()
  19.       adp = New OracleDataAdapter("SELECT empno, ename, 
  20.              job, mgr, hiredate, sal, comm, deptno FROM 
  21.              Emp", cn)
  22.       adp.Fill(ds, "Employees")
  23.       adp.Dispose()
  24.  
  25.       Me.DataGridView1.DataSource = ds
  26.       Me.DataGridView1.DataMember = "Departments"
  27.  
  28.       Me.DataGridView2.DataSource = 
  29.                                  ds.Tables("Employees")
  30.     Catch ex As Exception
  31.       'display if any error occurs
  32.       MessageBox.Show("Error: " & ex.Message)
  33.       'close the connection if it is still open
  34.       If cn.State = ConnectionState.Open Then
  35.         cn.Close()
  36.       End If
  37.     End Try
  38.   End Sub
  39. End Class
Copyright exforsys.com


From the above highlighted code, you can easily observe that we are retrieving two different result sets (identified by Departments and Employees) into the same dataset. The following code fragment creates the Departments data table:


adp = New OracleDataAdapter("SELECT deptno, dname,
loc FROM Dept", cn)
adp.Fill(ds, "Departments")
adp.Dispose()


The following code fragment creates the Employees data table:


adp = New OracleDataAdapter("SELECT empno, ename, job,
mgr, hiredate, sal, comm, deptno FROM Emp", cn)
adp.Fill(ds, "Employees")
adp.Dispose()


Those two result sets are automatically created as two data tables within the same dataset. Once the dataset is populated, we can present them with two different grids (two different methods) as follows:


Me.DataGridView1.DataSource = ds
Me.DataGridView1.DataMember = "Departments"
Me.DataGridView2.DataSource = ds.Tables("Employees")



The output for this code would look similar to the following figure:




Read Next: ODP.NET - Presenting Master-Detail Information Using a Dataset



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links