|
Page 1 of 2
ODP.NET - Retrieving Multiple Rows on to the Grid
In the previous section, we tried to retrieve only one row using OracleDataReader. In this section, we will try to retrieve more than one row (or a result set) and populate a DataGridView on a WinForm.
The following code lists out the details of all employees available in the emp table:
Imports Oracle.DataAccess.Client Public Class Form2 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 command object to work with SELECT Dim cmd As New OracleCommand(SQL, cn) 'open the connection cmd.Connection.Open() 'get the DataReader object from command object Dim rdr As OracleDataReader = _ cmd.ExecuteReader(CommandBehavior.CloseConnection) 'check if it has any rows If rdr.HasRows Then With Me.DataGridView1 'remove existing rows from grid .Rows.Clear() 'get the number of columns Dim ColumnCount As Integer = rdr.FieldCount 'add columns to the grid For i As Integer = 0 To ColumnCount - 1 .Columns.Add(rdr.GetName(i), rdr.GetName(i)) Next .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader 'loop through every row While rdr.Read 'get all row values into an array Dim objCells(ColumnCount - 1) As Object rdr.GetValues(objCells) 'add array as a row to grid .Rows.Add(objCells) End While End With Else 'display message if no rows found MessageBox.Show("Not found") Me.DataGridView1.Rows.Clear() End If 'clear up the resources rdr.Close() 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%20Form2%20%0A%20%20%20Private%20Sub%20btnGetEmployees_Click%28ByVal%20sender%20As%20%0A%20%20%20%20%20%20System.Object%2C%20ByVal%20e%20As%20System.EventArgs%29%20Handles%20%0A%20%20%20%20%20%20btnGetEmployees.Click%20%0A%20%20%20%20%20%20%27create%20connection%20to%20db%20%0A%20%20%20%20%20%20Dim%20cn%20As%20New%20OracleConnection%28%22Data%20Source%3Dxe%3B%20%0A%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%20Try%20%0A%20%20%20%20%20%20%20%20%20Dim%20SQL%20As%20String%20%0A%20%20%20%20%20%20%20%20%20%27build%20the%20SELECT%20statement%20%0A%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%20mgr%2C%20hiredate%2C%20sal%2C%20comm%2C%20deptno%20FROM%20emp%22%29%20%0A%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%20Dim%20cmd%20As%20New%20OracleCommand%28SQL%2C%20cn%29%20%0A%20%20%20%20%20%20%20%20%20%27open%20the%20connection%20%0A%20%20%20%20%20%20%20%20%20cmd.Connection.Open%28%29%20%0A%20%20%20%20%20%20%20%20%20%27get%20the%20DataReader%20object%20from%20command%20object%20%0A%20%20%20%20%20%20%20%20%20Dim%20rdr%20As%20OracleDataReader%20%3D%20_%20%0A%20%20%20%20%20%20%20%20%20cmd.ExecuteReader%28CommandBehavior.CloseConnection%29%20%0A%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%20If%20rdr.HasRows%20Then%20%0A%20%20%20%20%20%20%20%20%20%20%20%20With%20Me.DataGridView1%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27remove%20existing%20rows%20from%20grid%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.Rows.Clear%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27get%20the%20number%20of%20columns%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dim%20ColumnCount%20As%20Integer%20%3D%20rdr.FieldCount%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27add%20columns%20to%20the%20grid%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20For%20i%20As%20Integer%20%3D%200%20To%20ColumnCount%20-%201%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.Columns.Add%28rdr.GetName%28i%29%2C%20rdr.GetName%28i%29%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Next%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.AutoSizeColumnsMode%20%3D%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20DataGridViewAutoSizeColumnsMode.ColumnHeader%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27loop%20through%20every%20row%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20While%20rdr.Read%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27get%20all%20row%20values%20into%20an%20array%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dim%20objCells%28ColumnCount%20-%201%29%20As%20Object%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rdr.GetValues%28objCells%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27add%20array%20as%20a%20row%20to%20grid%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.Rows.Add%28objCells%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20End%20While%20%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20With%20%0A%20%20%20%20%20%20%20%20%20Else%20%0A%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%20MessageBox.Show%28%22Not%20found%22%29%20%0A%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%20End%20If%20%0A%20%20%20%20%20%20%20%20%20%27clear%20up%20the%20resources%20%0A%20%20%20%20%20%20%20%20%20rdr.Close%28%29%20%0A%20%20%20%20%20%20Catch%20ex%20As%20Exception%20%0A%20%20%20%20%20%20%20%20%20%27display%20if%20any%20error%20occurs%20%0A%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%27close%20the%20connection%20if%20it%20is%20still%20open%20%0A%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%20cn.Close%28%29%20%0A%20%20%20%20%20%20%20%20%20End%20If%20%0A%20%20%20%20%20%20End%20Try%20%0A%20%20%20End%20Sub%20%0AEnd%20Class%20
Except the highlighted section, the rest of the code is already explained as part of the previous section. You can observe that the SELECT statement now tries to retrieve all rows from emp as follows:
SQL = String.Format("SELECT empno, ename, job, mgr,
hiredate, sal, comm, deptno FROM emp")
Once the OracleDataReader is ready with rows, we need to start with clearing the rows already displayed in the DataGridView with the help of the following code:
With Me.DataGridView1
'remove existing rows from grid
.Rows.Clear()
Once the rows are cleared, the first issue is the header of the grid. The moment we add columns to the grid, the header row gets automatically populated (with the column names). Before adding columns to the header, we should know the number of columns being added (just for the loop iterations) with the FieldCount property of DataGridView. The following is the code fragment that finds the number of columns and adds the columns to DataGridView:
Dim ColumnCount As Integer = rdr.FieldCount
For i As Integer = 0 To ColumnCount - 1
.Columns.Add(rdr.GetName(i), rdr.GetName(i))
Next
All the columns get auto-sized based on the column header with the following statement:
.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.ColumnHeader
Once the columns are added, we need to read every successive row from the OracleDataReader and add it to the DataGridview. To add all column values at a time, we make use of the GetValues() method of OracleDataReader to push all the values in to an array and finally add the array itself as a row to the DataGridView. The following code fragment accomplishes this.
While rdr.Read 'get all row values into an array Dim objCells(ColumnCount - 1) As Object rdr.GetValues(objCells) 'add array as a row to grid .Rows.Add(objCells) End While
While%20rdr.Read%20%0A%20%20%20%20%20%27get%20all%20row%20values%20into%20an%20array%20%0A%20%20%20%20%20Dim%20objCells%28ColumnCount%20-%201%29%20As%20Object%20%0A%20%20%20%20%20rdr.GetValues%28objCells%29%20%0A%20%20%20%20%20%27add%20array%20as%20a%20row%20to%20grid%20%0A%20%20%20%20%20.Rows.Add%28objCells%29%20%0AEnd%20While%20
The output for this code would look similar to the following figure:

|