Exforsys

Home arrow Reviews arrow ODP.NET

ODP.NET - Populating a Dataset with a Single Data Table

Author: Packt Publishing     Published on: 5th Apr 2008    |   Last Updated on: 9th Apr 2008

ODP.NET - Populating a Dataset with a Single Data Table

A dataset is simply a group of data tables. These data tables can be identified with their own unique names within a dataset. You can also add relations between data tables available in a dataset.

Ads

The following code gives you the details of all employees available in the emp table by populating a dataset with only a single data table using OracleDataAdapter:

Sample Code
  1. Imports Oracle.DataAccess.Client
  2. Public Class Form6
  3.  
  4.   Private Sub btnGetEmployees_Click(ByVal sender As  
  5.   System.Object, ByVal e As System.EventArgs) Handles  
  6.   btnGetEmployees.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 SQL As String
  12.       'build the SELECT statement
  13.       SQL = String.Format("SELECT empno, ename, job,  
  14.              mgr, hiredate, sal, comm, deptno FROM emp")
  15.       'create the dataadapter object
  16.       Dim adp As New OracleDataAdapter(SQL, cn)
  17.       'create the offline datatable
  18.       Dim ds As New DataSet
  19.       'fill the data set with a data table named emp
  20.       adp.Fill(ds, "emp")
  21.       'clear up the resources and work offline
  22.       adp.Dispose()
  23.       'check if it has any rows
  24.       If ds.Tables("emp").Rows.Count > 0 Then
  25.         'simply bind datatable to grid
  26.         Me.DataGridView1.DataSource = ds.Tables("emp")
  27.       Else
  28.         'display message if no rows found
  29.         MessageBox.Show("Not found")
  30.         Me.DataGridView1.Rows.Clear()
  31.       End If
  32.     Catch ex As Exception
  33.       'display if any error occurs
  34.       MessageBox.Show("Error: " & ex.Message)
  35.       'close the connection if it is still open
  36.       If cn.State = ConnectionState.Open Then
  37.         cn.Close()
  38.       End If
  39.     End Try
  40.   End Sub
  41. End Class
Copyright exforsys.com


Ads

If you can observe the highlighted code in the above script, we are creating a new DataSet object, populating it with a DataTable named "emp" (which contains all the rows) and finally assigning the same DataTable to the grid. The output for the above code would look similar to the figure in the section Retrieving Multiple Rows into a Data Table Using OracleDataAdapter.



 
This tutorial is part of a ODP.NET tutorial series. Read it from the beginning and learn yourself.

ODP.NET

 

Comments