Exforsys.com
 
Home Tutorials ODP.NET
 

ODP.NET - Retrieving Typed Data

 

Retrieving Multiple Rows

Page 2 of 2


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:



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.



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





First Page: ODP.NET - Retrieving Typed Data


Read Next: ODP.NET - Filling a DataTable Using OracleDataReader



 

 

Comments



Post Your Comment:

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

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape