Exforsys.com
 
Home Tutorials ODP.NET
 

ODP.NET - Retrieving Data Using OracleDataReader

 

ODP.NET - Retrieving Data Using OracleDataReader

Page 1 of 2

ODP.NET - Retrieving Data Using OracleDataReader

OracleDataReader is simply a read-only and forward-only result set. It works only if the database connection is open and it makes sure that the connection is open while you are retrieving data. As the data that it retrieves is read-only, it is a bit faster than any other method to retrieve data from Oracle.


ODP.NET - Retrieving Data Using OracleDataReader

OracleDataReader is simply a read-only and forward-only result set. It works only if the database connection is open and it makes sure that the connection is open while you are retrieving data. As the data that it retrieves is read-only, it is a bit faster than any other method to retrieve data from Oracle.



You need to work with OracleCommand together with OracleConnection to get access to OracleDataReader. There is an ExecuteReader method in the OracleCommand class, which gives you the OracleDataReader.


Retrieving a Single Row of Information

Let us start by retrieving a single row from Oracle database using ODP.NET and populate the data into few textboxes on a WinForm.


To connect to and work with Oracle database, we need to start with OracleConnection. Once a connection to the database is established, we need to issue a SELECT statement to retrieve some information from the database. A query (or any SQL command) can be executed with the help of an OracleCommand object. Once the SELECT statement gets executed, we can use OracleDataReader to retrieve the information.


The following code accepts an employee number from the user and gives you the details of that employee:


Sample Code
  1. Imports Oracle.DataAccess.Client
  2.  
  3. Public Class Form1
  4.  
  5.   Private Sub btnGetEmployee_Click(ByVal sender As 
  6.     System.ObjectByVal e As System.EventArgs) Handles 
  7.     btnGetEmployee.Click
  8.     'create connection to db
  9.     Dim cn As New OracleConnection("Data Source=xe; _
  10.                            User Id=scott;Password=tiger")
  11.     Try
  12.       Dim SQL As String
  13.       'build the SELECT statement
  14.  
  15.       SQL = String.Format("SELECT ename, sal, job FROM 
  16.                  emp WHERE empno={0}"Me.txtEmpno.Text)
  17.       'create command object to work with SELECT
  18.       Dim cmd As New OracleCommand(SQL, cn)
  19.       'open the connection
  20.       cmd.Connection.Open()
  21.       'get the DataReader object from command object
  22.       Dim rdr As OracleDataReader = _
  23.       cmd.ExecuteReader(CommandBehavior.CloseConnection)
  24.       'check if it has any rows
  25.       If rdr.HasRows Then
  26.         'read the first row
  27.         rdr.Read()
  28.         'extract the details
  29.         Me.txtEname.Text = rdr("ename")
  30.         Me.txtSal.Text = rdr("sal")
  31.         Me.txtJob.Text = rdr("job")
  32.       Else
  33.         'display message if no rows found
  34.         MessageBox.Show("Not found")
  35.       End If
  36.       'clear up the resources
  37.       rdr.Close()
  38.     Catch ex As Exception
  39.       'display if any error occurs
  40.       MessageBox.Show("Error: " & ex.Message)
  41.       'close the connection if it is still open
  42.       If cn.State = ConnectionState.Open Then
  43.         cn.Close()
  44.       End If
  45.     End Try
  46.   End Sub
  47.  
  48. End Class
Copyright exforsys.com


As explained earlier, the above program creates an OracleConnection object as follows:


Dim cn As New OracleConnection('Data Source=xe; _
User Id=scott;Password=tiger')


Next, we need to create an OracleCommand object by providing a SELECT query and the connection object (through which it can connect to the database): 


Dim SQL As String
SQL = String.Format('SELECT ename, sal, job FROM
emp WHERE empno={0}', Me.txtEmpno.Text)
Dim cmd As New OracleCommand(SQL, cn)


Once the OracleCommand object is created, it is time to open the connection and execute the SELECT query. The following does this:


cmd.Connection.Open()
Dim rdr As OracleDataReader = _
cmd.ExecuteReader(CommandBehavior.CloseConnection)


You must observe that the query gets executed using the ExecuteReader method of OracleCommand object, which in turn returns an OracleDataReader object. In the above statement, the ExecuteReader method is specified with CommandBehavior. CloseConnection, which simply closes the database connection once the OracleDataReader and OracleCommand are disposed.


We can use the HasRows property of OracleDataReader to test whether the reader retrieved any rows or not. If any rows are retrieved, we can read each successive row using the Read method of OracleDataReader. The Read method returns a Boolean value to indicate whether it has successfully read a row or not. Once the Read succeeds, we can retrieve each value in the row with the column name as follows:


Sample Code
  1. If rdr.HasRows Then
  2.   'read the first row
  3.   rdr.Read()
  4.   'extract the details
  5.   Me.txtEname.Text = rdr("ename")
  6.   Me.txtSal.Text = rdr("sal")
  7.   Me.txtJob.Text = rdr("job")
  8. Else
  9.   'display message if no rows found
  10.   MessageBox.Show("Not found")
  11. End If
Copyright exforsys.com


Finally, we close the OracleDataReader object using the Close method as follows:


rdr.Close()


If it could read successfully, the output for this code would look similar to the following figure:



Next Page: Using keyword in Visual Basic 2005


Read Next: ODP.NET - Retrieving Multiple Rows on to the Grid



 

 

Comments


sneha said:

  nice code
January 29, 2009, 5:03 am

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