Exforsys

Home arrow Reviews arrow ODP.NET

ODP.NET - Working with DataTableReader

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

ODP.NET - Working with DataTableReader

DataTableReader is complementary to a DataTable object, and is mainly used as a type of Data Reader in the disconnected mode. The following is the modified code:

Ads

Sample Code
  1. 'create connection to db
  2. Dim cn As New OracleConnection("Data Source=xe; _
  3.                           User Id=scott;Password=tiger")
  4. Try
  5.   Dim SQL As String
  6.   'build the SELECT statement
  7.   SQL = String.Format("SELECT ename, sal, job FROM emp 
  8.                      WHERE empno={0}"Me.txtEmpno.Text)
  9.   'create the DataAdapter object
  10.   Dim adp As New OracleDataAdapter(SQL, cn)
  11.   'create the offline datatable
  12.   Dim dt As New DataTable
  13.   'fill the data table with rows
  14.   adp.Fill(dt)
  15.   'clear up the resources and work offline
  16.   adp.Dispose()
  17.   Dim dtr As DataTableReader = dt.CreateDataReader
  18.  
  19.   'check if it has any rows
  20.   If dtr.HasRows Then
  21.     'read the first row
  22.     dtr.Read()
  23.     'extract the details
  24.     Me.txtEname.Text = dtr("ename")
  25.     Me.txtSal.Text = dtr("sal")
  26.     Me.txtJob.Text = dtr("job")
  27.   Else
  28.     'display message if no rows found
  29.     MessageBox.Show("Not found")
  30.   End If
  31.  
  32. Catch ex As Exception
  33.   'display if any error occurs
  34.   MessageBox.Show("Error: " & ex.Message)
  35.  'close the connection if it is still open
  36.   If cn.State = ConnectionState.Open Then
  37.     cn.Close()
  38.   End If
  39. End Try
Copyright exforsys.com


Ads

You can observe the highlighted code, which creates a DataTableReader object by calling the CreateDataReader method related to the DataTable object. Once the DataTableReader is created, we can directly retrieve the column values with the specified column names as follows:

Me.txtEname.Text = dtr("ename")
Me.txtSal.Text = dtr("sal")
Me.txtJob.Text = dtr("job")



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

ODP.NET

 

Comments