Free Training
C Language   |   CSS   |   MainFrame   |   VBScript   |   PHP   |   XML   |   C++ Tutorials   |   Ajax   |   JavaScript   |   CSS3   |   UML   |   jQuery   |   Microsoft AJAX

Sponsored Links

ODP.NET Tutorials

 
Home Tutorials ODP.NET
 

ODP.NET - Working with DataTableReader

 

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:



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



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")



Read Next: ODP.NET - Populating a Dataset with a Single Data Table



 

 

Comments



Post Your Comment:

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

Sponsored Links