Exforsys.com
 
Home Tutorials ODP.NET
 

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:


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



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



Read Next: ODP.NET - Techniques to Improve Performance while Retrieving Data



 

 

Comments



Post Your Comment:

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

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape