ODP.NET - Filling a DataTable Using OracleDataReader
So far, we have been filling data tables using OracleDataAdapter. ADO.NET 2.0 gives us the flexibility to fill a data table using OracleDataReader as well. The following code gives you the details of all employees available in the emp table by filling a data table using an OracleDataReader:
Dim cn As New OracleConnection("Data Source=xe; _ User Id=scott;Password=tiger") Try Dim SQL As String Dim dt As New DataTable '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 'simply bind datatable to grid dt.Load(rdr, LoadOption.OverwriteChanges) Me.DataGridView1.DataSource = dt Else 'display message if no rows found MessageBox.Show("Not found") Me.DataGridView1.Rows.Clear() End If 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
Dim%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%20Try%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20SQL%20As%20String%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%27build%20the%20SELECT%20statement%20%0A%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%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%27create%20command%20object%20to%20work%20with%20SELECT%20%0A%20%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%20%27open%20the%20connection%20%0A%20%20%20%20%20%20%20%20%20%20cmd.Connection.Open%28%29%20%0A%20%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%20%20Dim%20rdr%20As%20OracleDataReader%20%3D%20_%20%0A%20%20%20%20%20%20%20%20%20%20cmd.ExecuteReader%28CommandBehavior.CloseConnection%29%20%0A%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%20If%20rdr.HasRows%20Then%20%0A%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%20dt.Load%28rdr%2C%20LoadOption.OverwriteChanges%29%20%0A%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%20Else%20%0A%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%20MessageBox.Show%28%22Not%20found%22%29%20%0A%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%20End%20If%20%0A%20%20%20%20%20%20%20%20%20%20rdr.Close%28%29%20%0A%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%20End%20Try
Once the OracleConnection and OracleDataReader are created, we need to create and fill a DataTable object using OracleDataReader itself. The following is the statement that creates a DataTable object:
Dim dt As New DataTable
To fill the above DataTable object with respect to OracleDataReader, we can directly use the Load method of DataTable, which accepts a DataReader object and the type of LoadOption. The following statement loads the content of an OracleDataReader into a DataTable object with a LoadOption as OverwriteChanges (overwrites all the modifications that are available as part of the DataTable object):
dt.Load(rdr, LoadOption.OverwriteChanges)
Trackback(0)
|