alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow ODP.NET arrow ODP.NET - Working with DataTableReader
Site Search


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:

  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. 'check if it has any rows
  19. If dtr.HasRows Then
  20. 'read the first row
  21. dtr.Read()
  22. 'extract the details
  23. Me.txtEname.Text = dtr("ename")
  24. Me.txtSal.Text = dtr("sal")
  25. Me.txtJob.Text = dtr("job")
  26. Else
  27. 'display message if no rows found
  28. MessageBox.Show("Not found")
  29. End If
  30. Catch ex As Exception
  31. 'display if any error occurs
  32. MessageBox.Show("Error: " & ex.Message)
  33. 'close the connection if it is still open
  34. If cn.State = ConnectionState.Open Then
  35. cn.Close()
  36. End If
  37. End Try
 

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


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< 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