Exforsys.com
 
Home Tutorials ODP.NET
 

ODP.NET - Retrieving Data Using OracleDataReader

 

Using keyword in Visual Basic 2005

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:



Sample Code
  1. Using cn As New OracleConnection("Data Source=xe;
  2.                       User Id=scott;Password=tiger")
  3.   Try
  4.     cn.Open()
  5.     Dim SQL As String
  6.     SQL = String.Format("SELECT ename, sal, 
  7.      job FROM emp WHERE empno={0}"Me.txtEmpno.Text)
  8.     Using cmd As New OracleCommand(SQL, cn)
  9.     Using rdr As OracleDataReader = cmd.ExecuteReader
  10.         If rdr.HasRows Then
  11.           'read the first row
  12.           rdr.Read()
  13.           'extract the details
  14.           Me.txtEname.Text = rdr("ename")
  15.           Me.txtSal.Text = rdr("sal")
  16.           Me.txtJob.Text = rdr("job")
  17.         Else
  18.           'display message if no rows found
  19.           MessageBox.Show("Not found")
  20.         End If
  21.       End Using
  22.     End Using
  23.   Catch ex As Exception
  24.     MessageBox.Show("Error: " & ex.Message)
  25.     If cn.State = ConnectionState.Open Then
  26.       cn.Close()
  27.     End If
  28.   End Try
  29. End Using
Copyright exforsys.com


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.




First Page: ODP.NET - Retrieving Data Using OracleDataReader


Read Next: ODP.NET - Retrieving Multiple Rows on to the Grid



 

 

Comments


sneha said:

  nice code
January 29, 2009, 5:03 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape