|
ODP.NET - Working with DataTableReader |
ODP.NET - Working with DataTableReader
DataTableReader is complementary to a DataTable object, and is mainly used as a type of Data Reader in the disconnected mode. The following is the modified code:
'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 ename, sal, job FROM emp WHERE empno={0}", Me.txtEmpno.Text) '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() Dim dtr As DataTableReader = dt.CreateDataReader 'check if it has any rows If dtr.HasRows Then 'read the first row dtr.Read() 'extract the details Me.txtEname.Text = dtr("ename") Me.txtSal.Text = dtr("sal") Me.txtJob.Text = dtr("job") Else 'display message if no rows found MessageBox.Show("Not found") 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
%27create%20connection%20to%20db%20%0ADim%20cn%20As%20New%20OracleConnection%28%22Data%20Source%3Dxe%3B%20_%20%0A%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%20%27build%20the%20SELECT%20statement%20%0A%20%20%20%20%20%20%20%20%20%20SQL%20%3D%20String.Format%28%22SELECT%20ename%2C%20sal%2C%20job%20FROM%20emp%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20WHERE%20empno%3D%7B0%7D%22%2C%20Me.txtEmpno.Text%29%20%0A%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%20Dim%20adp%20As%20New%20OracleDataAdapter%28SQL%2C%20cn%29%20%0A%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%20Dim%20dt%20As%20New%20DataTable%20%0A%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%20adp.Fill%28dt%29%20%0A%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%20adp.Dispose%28%29%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20dtr%20As%20DataTableReader%20%3D%20dt.CreateDataReader%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%20dtr.HasRows%20Then%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27read%20the%20first%20row%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dtr.Read%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%27extract%20the%20details%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Me.txtEname.Text%20%3D%20dtr%28%22ename%22%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Me.txtSal.Text%20%3D%20dtr%28%22sal%22%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Me.txtJob.Text%20%3D%20dtr%28%22job%22%29%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%20End%20If%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%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
You can observe the highlighted code, which creates a DataTableReader object by calling the CreateDataReader method related to the DataTable object. Once the DataTableReader is created, we can directly retrieve the column values with the specified column names as follows:
Me.txtEname.Text = dtr("ename")
Me.txtSal.Text = dtr("sal")
Me.txtJob.Text = dtr("job")
Trackback(0)
|