Exforsys

Home arrow Reviews arrow ODP.NET

Retrieving Multiple Rows

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

ODP.NET - Retrieving Typed Data

Retrieving Multiple Rows into a DataTable Using OracleDataAdapter

Now that we understand about OracleDataAdapter, let us try to use it to retrieve all the employees available in the emp table:

Ads

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


Once the OracleConnection is established, we need to start with the OracleDataAdapter object as follows:

SQL = String.Format("SELECT empno, ename, job,
mgr, hiredate, sal, comm, deptno FROM emp")
Dim adp As New OracleDataAdapter(SQL, cn)

You can understand from the above that OracleDataAdapter can be used directly with a SELECT statement. You can also specify an OracleCommand object in place of a SELECT statement if necessary.

To place data offline, we need to either work with DataSet or DataTable objects. In this scenario, we will deal with a DataTable object, and it is created as follows:

Dim dt As New DataTable

Once the DataTable object is created, we need to fill up all the rows using the OracleDataAdapter object as follows:


adp.Fill(dt)

Once all the rows are available in the DataTable object (which will always be in memory), we can close (dispose) the OracleDataAdapter using the following statement:

adp.Dispose()

The DataTable object contains a collection of DataRow objects corresponding to each row populated into it. We can retrieve the number of rows available in the DataTable object using the DataTable.Rows.Count property as follows:

Sample Code
  1. If dt.Rows.Count > 0 Then
  2.         'simply bind datatable to grid
  3.         Me.DataGridView1.DataSource = dt
  4.       Else
  5.         'display message if no rows found
  6.         MessageBox.Show("Not found")
  7.         Me.DataGridView1.Rows.Clear()
  8. End If
Copyright exforsys.com


In the above code fragment, we are assigning the DataTable object as DataSource to DataGridView. This would automatically populate entire DataGridView with all the column names (as part of the header) and all rows.

Ads

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



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

ODP.NET

 

Comments