Exforsys

Home arrow Reviews arrow ODP.NET

ODP.NET - Retrieving a Single Row of Information Using OracleDataAdapter

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

ODP.NET - Retrieving a Single Row of Information Using OracleDataAdapter

In the previous example, we worked with a set of rows in the DataTable object. Now, we shall work with a particular row using the DataTable object. The following code accepts an employee number from the user and gives you the details of that employee:

Ads

Sample Code
  1. Imports Oracle.DataAccess.Client
  2.  
  3. Public Class Form3
  4.  
  5.   Private Sub btnGetEmployee_Click(ByVal sender As  
  6.   System.Object, ByVal 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.       SQL = String.Format("SELECT ename, sal, job FROM  
  15.                emp WHERE empno={0}", Me.txtEmpno.Text)
  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.  
  25.       'check if it has any rows
  26.  
  27.       If dt.Rows.Count > 0 Then
  28.         'extract the details
  29.         Me.txtEname.Text = dt.Rows(0)("ename")
  30.         Me.txtSal.Text = dt.Rows(0)("sal")
  31.         Me.txtJob.Text = dt.Rows(0)("job")
  32.       Else
  33.         'display message if no rows found
  34.         MessageBox.Show("Not found")
  35.       End If
  36.  
  37.     Catch ex As Exception
  38.       'display if any error occurs
  39.       MessageBox.Show("Error: " & ex.Message)
  40.       'close the connection if it is still open
  41.       If cn.State = ConnectionState.Open Then
  42.         cn.Close()
  43.       End If
  44.     End Try
  45.   End Sub
  46. End Class
Copyright exforsys.com


Once the DataTable object is filled using OracleDataAdapter, we can directly retrieve a particular row using the row index. Once the row is fetched, we extract column values by providing column names for the rows as follows:

Me.txtEname.Text = dt.Rows(0)("ename")
Me.txtSal.Text = dt.Rows(0)("sal")
Me.txtJob.Text = dt.Rows(0)("job")

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