Presenting Master-Detail Information Using a Dataset
As mentioned before, a DataSet object can have its own relations between data
tables existing in it. We can add these relations dynamically at the client
side (within an application), to represent master-detail (or hierarchical)
information. The following code gives the list of employees (in the bottom
grid) based on the department you choose in the top grid:
Imports Oracle.DataAccess.Client
Public Class Form8
Private Sub btnData_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnData.Click
'create connection to db
Dim cn As New OracleConnection("Data Source=xe; _
UserId=scott;Password=tiger")
Try
Dim ds As New DataSet
Dim adp As OracleDataAdapter
adp = New OracleDataAdapter("SELECT deptno,
dname, loc FROM Dept", cn)
adp.Fill(ds, "Departments")
adp.Dispose()
adp = New OracleDataAdapter("SELECT empno, ename,
job, mgr, hiredate, sal, comm, deptno FROM
Emp", cn)
adp.Fill(ds, "Employees")
adp.Dispose()
ds.Relations.Add(New DataRelation("FK_Emp_Dept",
ds.Tables("Departments").Columns("Deptno"),
ds.Tables("Employees").Columns("Deptno")))
Dim bsMaster As New BindingSource(ds, _
"Departments")
Dim bsChild As New BindingSource(bsMaster, _
"FK_Emp_Dept")
Me.DataGridView1.DataSource = bsMaster
Me.DataGridView2.DataSource = bsChild
Catch ex As Exception
'display if any error occurs
MessageBox.Show("Error: " & ex.Message)
'close the connection if it is still open
If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Try
End Sub
End Class
|
Book Excerpt: Retrieving Data from Oracle
Using ODP.NET
Chapter Contents
This excerpt from
ODP.NET Developer's Guide: Oracle Database 10g Development with Visual Studio
2005 and the Oracle Data Provider for .NET by Jagadish Chatarji
Pulakhandam, Sunitha Paruchuri, is printed with permission from
Packt Publishing, Copyright 2007.
|