Exforsys

Home arrow Reviews arrow ODP.NET

ODP.NET - Working with OracleDataAdapter with OracleCommand

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

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.

Ads

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

Sample Code
  1. Imports Oracle.DataAccess.Client
  2.  
  3. Public Class Form10
  4.  
  5.   Private Sub btnGetEmployees_Click_1(ByVal sender As 
  6.  
  7.   System.ObjectByVal e As System.EventArgs) Handles 
  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.  
  35.   End Sub
  36. End Class
Copyright exforsys.com


Ads

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



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

ODP.NET

 

Comments