|
ODP.NET - Retrieving Data Using OracleDataReader |
|
Page 2 of 2
Using "Using" for Simplicity
The above program can be made simple by using the Using statement together with ODP.NET classes as follows:
Using cn As New OracleConnection("Data Source=xe; User Id=scott;Password=tiger") Try cn.Open() Dim SQL As String SQL = String.Format("SELECT ename, sal, job FROM emp WHERE empno={0}", Me.txtEmpno.Text) Using cmd As New OracleCommand(SQL, cn) Using rdr As OracleDataReader = cmd.ExecuteReader If rdr.HasRows Then 'read the first row rdr.Read() 'extract the details Me.txtEname.Text = rdr("ename") Me.txtSal.Text = rdr("sal") Me.txtJob.Text = rdr("job") Else 'display message if no rows found MessageBox.Show("Not found") End If End Using End Using Catch ex As Exception MessageBox.Show("Error: " & ex.Message) If cn.State = ConnectionState.Open Then cn.Close() End If End Try End Using
Using%20cn%20As%20New%20OracleConnection%28%22Data%20Source%3Dxe%3B%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%20Try%20%0A%20%20%20%20%20%20%20%20%20%20cn.Open%28%29%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%20SQL%20%3D%20String.Format%28%22SELECT%20ename%2C%20sal%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20job%20FROM%20emp%20WHERE%20empno%3D%7B0%7D%22%2C%20Me.txtEmpno.Text%29%20%0A%20%20%20%20%20%20%20%20%20%20Using%20cmd%20As%20New%20OracleCommand%28SQL%2C%20cn%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20Using%20rdr%20As%20OracleDataReader%20%3D%20cmd.ExecuteReader%20%0A%20%20%20%20%20%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%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%20%20%20%20%20%20rdr.Read%28%29%20%0A%20%20%20%20%20%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%20%20%20%20%20%20Me.txtEname.Text%20%3D%20rdr%28%22ename%22%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Me.txtSal.Text%20%3D%20rdr%28%22sal%22%29%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Me.txtJob.Text%20%3D%20rdr%28%22job%22%29%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%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%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%20End%20If%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20End%20Using%20%0A%20%20%20%20%20%20%20%20%20%20End%20Using%20%0A%20%20%20%20%20Catch%20ex%20As%20Exception%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%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%20%0AEnd%20Using
The Using keyword is new in Visual Basic 2005, which internally generates try and finally blocks around the object being allocated and calls Dispose() for you saving you the hassle of manually creating it.
The objects created using the Using keyword are automatically erased (and respective resources would be automatically cleared) from the memory once it is out of using scope. Even though it is very flexible to use the Using statement, for the sake of clarity, we will go without using it in the examples of this book.
Trackback(0)

|