|
Page 2 of 2
Retrieving Multiple Rows into a DataTable Using OracleDataAdapter
Now that we understand about OracleDataAdapter, let us try to use it to retrieve all the employees available in the emp table:
Imports Oracle.DataAccess.Client Public Class Form4 Private Sub btnGetEmployees_Click(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 Dim SQL As String 'build the SELECT statement SQL = String.Format("SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno FROM emp") 'create the dataadapter object Dim adp As New OracleDataAdapter(SQL, cn) 'create the offline datatable Dim dt As New DataTable 'fill the data table with rows adp.Fill(dt) 'clear up the resources and work offline adp.Dispose() 'check if it has any rows If dt.Rows.Count > 0 Then 'simply bind datatable to grid Me.DataGridView1.DataSource = dt Else 'display message if no rows found MessageBox.Show("Not found") Me.DataGridView1.Rows.Clear() End If 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%0APublic%20Class%20Form4%20%0A%0A%20%20%20%20%20Private%20Sub%20btnGetEmployees_Click%28ByVal%20sender%20As%0A%20%20%20%20%20System.Object%2C%20ByVal%20e%20As%20System.EventArgs%29%20Handles%20%0A%20%20%20%20%20btnGetEmployees.Click%20%0A%20%20%20%20%20%20%20%20%20%20%27create%20connection%20to%20db%20%0A%20%20%20%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%20User%20Id%3Dscott%3BPassword%3Dtiger%22%29%20%0A%20%20%20%20%20%20%20%20%20%20Try%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dim%20SQL%20As%20String%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27build%20the%20SELECT%20statement%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SQL%20%3D%20String.Format%28%22SELECT%20empno%2C%20ename%2C%20job%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mgr%2C%20hiredate%2C%20sal%2C%20comm%2C%20deptno%20FROM%20emp%22%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27create%20the%20dataadapter%20object%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dim%20adp%20As%20New%20OracleDataAdapter%28SQL%2C%20cn%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27create%20the%20offline%20datatable%20%0A%20%20%20%20%20%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%20%20%20%20%20%27fill%20the%20data%20table%20with%20rows%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adp.Fill%28dt%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27clear%20up%20the%20resources%20and%20work%20offline%20%0A%20%20%20%20%20%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%20%20%20%20%20%27check%20if%20it%20has%20any%20rows%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20If%20dt.Rows.Count%20%3E%200%20Then%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27simply%20bind%20datatable%20to%20grid%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Me.DataGridView1.DataSource%20%3D%20dt%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Else%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27display%20message%20if%20no%20rows%20found%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MessageBox.Show%28%22Not%20found%22%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Me.DataGridView1.Rows.Clear%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20End%20If%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Catch%20ex%20As%20Exception%20%0A%20%20%20%20%20%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%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%20%20%20%20%20%27close%20the%20connection%20if%20it%20is%20still%20open%20%0A%20%20%20%20%20%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%20%20%20%20%20%20cn.Close%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20End%20If%20%0A%20%20%20%20%20%20%20%20%20%20End%20Try%20%0A%20%20%20%20%20End%20Sub%20%0AEnd%20Class
Once the OracleConnection is established, we need to start with the OracleDataAdapter object as follows:
SQL = String.Format("SELECT empno, ename, job,
mgr, hiredate, sal, comm, deptno FROM emp")
Dim adp As New OracleDataAdapter(SQL, cn)
You can understand from the above that OracleDataAdapter can be used directly with a SELECT statement. You can also specify an OracleCommand object in place of a SELECT statement if necessary.
To place data offline, we need to either work with DataSet or DataTable objects. In this scenario, we will deal with a DataTable object, and it is created as follows:
Dim dt As New DataTable
Once the DataTable object is created, we need to fill up all the rows using the OracleDataAdapter object as follows:
adp.Fill(dt)
Once all the rows are available in the DataTable object (which will always be in memory), we can close (dispose) the OracleDataAdapter using the following statement:
adp.Dispose()
The DataTable object contains a collection of DataRow objects corresponding to each row populated into it. We can retrieve the number of rows available in the DataTable object using the DataTable.Rows.Count property as follows:
If dt.Rows.Count > 0 Then 'simply bind datatable to grid Me.DataGridView1.DataSource = dt Else 'display message if no rows found MessageBox.Show("Not found") Me.DataGridView1.Rows.Clear() End If
If%20dt.Rows.Count%20%3E%200%20Then%20%0A%20%20%20%27simply%20bind%20datatable%20to%20grid%20%0A%20%20%20Me.DataGridView1.DataSource%20%3D%20dt%20%0AElse%20%0A%20%20%20%27display%20message%20if%20no%20rows%20found%20%0A%20%20%20MessageBox.Show%28%22Not%20found%22%29%20%0A%20%20%20Me.DataGridView1.Rows.Clear%28%29%20%0AEnd%20If
In the above code fragment, we are assigning the DataTable object as DataSource to DataGridView. This would automatically populate entire DataGridView with all the column names (as part of the header) and all rows.
The output for the above code would look similar to the following figure:

Trackback(0)

|