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


ODP.NET - Working with OracleDataAdapter with OracleCommand

ODP.NET - Working with OracleDataAdapter together with OracleCommand

In the previous examples, we worked with OracleDataAdapter by directly specifying SQL statements. You can also pass OracleCommand to OracleDataAdapter. This is very useful if you deal with stored procedures (covered in Chapter 5) or bind variables together with OracleDataAdapter.

The following is a simple example that uses OracleCommand together with OracleDataAdapter:

  1. Imports Oracle.DataAccess.Client
  2.  
  3. Public Class Form10
  4.  
  5. Private Sub btnGetEmployees_Click_1(ByVal sender As
  6. System.Object, ByVal e As System.EventArgs) Handles
  7.  
  8. btnGetEmployees.Click
  9. 'create connection to db
  10. Dim cn As New OracleConnection("Data Source=xe; _
  11. User Id=scott;Password=tiger")
  12. Try
  13. 'create command object to work with SELECT
  14. Dim cmd As New OracleCommand("SELECT empno, _
  15. ename, job, mgr, hiredate, sal, comm, deptno _
  16. FROM emp", cn)
  17. 'create DataAdapter from command
  18. Dim adp As New OracleDataAdapter(cmd)
  19. 'create the offline data table
  20. Dim dt As New DataTable
  21. 'fill the data table with data and clear resources
  22. adp.Fill(dt)
  23. adp.Dispose()
  24. 'display the data
  25. Me.DataGridView1.DataSource = dt
  26. Catch ex As Exception
  27. 'display if any error occurs
  28. MessageBox.Show("Error: " & ex.Message)
  29. 'close the connection if it is still open
  30. If cn.State = ConnectionState.Open Then
  31. cn.Close()
  32. End If
  33. End Try
  34. End Sub
  35. End Class
 

You can observe from the above highlighted code that we created an OracleCommand object, and the OracleDataAdapter can accept OracleCommand as a parameter.


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