alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow ODP.NET arrow ODP.NET - Retrieving Data Using OracleDataReader
Site Search


ODP.NET - Retrieving Data Using OracleDataReader
Article Index
ODP.NET - Retrieving Data Using OracleDataReader
Using keyword in Visual Basic 2005

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:

  1. Imports Oracle.DataAccess.Client
  2. Public Class Form1
  3.  
  4. Private Sub btnGetEmployee_Click(ByVal sender As
  5. System.Object, ByVal e As System.EventArgs) Handles
  6. btnGetEmployee.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 ename, sal, job FROM
  14.        emp WHERE empno={0}", Me.txtEmpno.Text)
  15.    'create command object to work with SELECT
  16.    Dim cmd As New OracleCommand(SQL, cn)
  17.    'open the connection
  18.    cmd.Connection.Open()
  19.    'get the DataReader object from command object
  20.    Dim rdr As OracleDataReader = _
  21.    cmd.ExecuteReader(CommandBehavior.CloseConnection)
  22.    'check if it has any rows
  23.    If rdr.HasRows Then
  24.     'read the first row
  25.     rdr.Read()
  26.     'extract the details
  27.     Me.txtEname.Text = rdr("ename")
  28.     Me.txtSal.Text = rdr("sal")
  29.     Me.txtJob.Text = rdr("job")
  30.    Else
  31.     'display message if no rows found
  32.     MessageBox.Show("Not found")
  33.    End If
  34.    'clear up the resources
  35.    rdr.Close()
  36. Catch ex As Exception
  37. 'display if any error occurs
  38. MessageBox.Show("Error: " & ex.Message)
  39. 'close the connection if it is still open
  40. If cn.State = ConnectionState.Open Then
  41. cn.Close()
  42. End If
  43. End Try
  44. End Sub
  45. End Class
 

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:

  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
 

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:



 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape