|
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:
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
Imports%20Oracle.DataAccess.Client%20%0A%0APublic%20Class%20Form10%20%0A%0A%20%20%20%20Private%20Sub%20btnGetEmployees_Click_1%28ByVal%20sender%20As%20%0A%20%20%20%20System.Object%2C%20ByVal%20e%20As%20System.EventArgs%29%20Handles%20%0A%0A%20%20%20%20btnGetEmployees.Click%20%0A%20%20%20%20%20%20%20%27create%20connection%20to%20db%20%0A%20%20%20%20%20%20%20Dim%20cn%20As%20New%20OracleConnection%28%22Data%20Source%3Dxe%3B%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20User%20Id%3Dscott%3BPassword%3Dtiger%22%29%20%0A%20%20%20%20%20%20%20Try%20%0A%20%20%20%20%20%20%20%20%20%20%27create%20command%20object%20to%20work%20with%20SELECT%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20cmd%20As%20New%20OracleCommand%28%22SELECT%20empno%2C%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20ename%2C%20job%2C%20mgr%2C%20hiredate%2C%20sal%2C%20comm%2C%20deptno%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20FROM%20emp%22%2C%20cn%29%20%0A%20%20%20%20%20%20%20%20%20%20%27create%20DataAdapter%20from%20command%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20adp%20As%20New%20OracleDataAdapter%28cmd%29%20%0A%20%20%20%20%20%20%20%20%20%20%27create%20the%20offline%20data%20table%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20dt%20As%20New%20DataTable%20%0A%20%20%20%20%20%20%20%20%20%20%27fill%20the%20data%20table%20with%20data%20and%20clear%20resources%20%0A%20%20%20%20%20%20%20%20%20%20adp.Fill%28dt%29%20%0A%20%20%20%20%20%20%20%20%20%20adp.Dispose%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%27display%20the%20data%20%0A%20%20%20%20%20%20%20%20%20%20Me.DataGridView1.DataSource%20%3D%20dt%20%0A%20%20%20%20%20%20%20Catch%20ex%20As%20Exception%20%0A%20%20%20%20%20%20%20%20%20%20%27display%20if%20any%20error%20occurs%20%0A%20%20%20%20%20%20%20%20%20%20MessageBox.Show%28%22Error%3A%20%22%20%26amp%3B%20ex.Message%29%20%0A%20%20%20%20%20%20%20%20%20%20%27close%20the%20connection%20if%20it%20is%20still%20open%20%0A%20%20%20%20%20%20%20%20%20%20If%20cn.State%20%3D%20ConnectionState.Open%20Then%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20cn.Close%28%29%20%0A%20%20%20%20%20%20%20%20%20%20End%20If%20%0A%20%20%20%20%20%20%20End%20Try%20%0A%20%20%20End%20Sub%20%20%20%20%20%20%20%20%20%20%20%0AEnd%20Class
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)
|