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
Imports Oracle.DataAccess.Client
Public Class Form10
Private Sub btnGetEmployees_Click_1(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnGetEmployees.Click
'create connection to db
Dim cn As New OracleConnection("Data Source=xe; _
User Id=scott;Password=tiger")
Try
'create command object to work with SELECT
Dim cmd As New OracleCommand("SELECT empno, _
ename, job, mgr, hiredate, sal, comm, deptno _
FROM emp", cn)
'create DataAdapter from command
Dim adp As New OracleDataAdapter(cmd)
'create the offline data table
Dim dt As New DataTable
'fill the data table with data and clear resources
adp.Fill(dt)
adp.Dispose()
'display the data
Me.DataGridView1.DataSource = dt
Catch ex As Exception
'display if any error occurs
MessageBox.Show("Error: " & ex.Message)
'close the connection if it is still open
If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Try
End Sub
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.