alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow ODP.NET arrow ODP.NET - OracleCommand Object
Site Search


ODP.NET - OracleCommand Object

ODP.NET - More About the OracleCommand Object

Till now, we have seen OracleCommand working with OracleDataReader. OracleCommand is not simply meant for OracleDataReader. It has got a lot of functionality for itself. Let us see few of the most commonly used features of OracleCommand in this section. We will further go into depth in subsequent sections and chapters.

Retrieving a Single Value from the Database

As we already covered working with single or multiple rows, we need to work on retrieving a single value from database very effectively. We have already retrieved row values in our previous examples, but those examples are more suitable when you are trying to deal with entire rows.

OracleCommand is equipped with a method called ExecuteScalar, which is mainly used to retrieve single values from the database very efficiently thus improving the performance. The following example focuses on this:

  1. Imports Oracle.DataAccess.Client
  2.  
  3. Public Class Form9
  4. Private Sub btnEmployeeCount_Click(ByVal sender As
  5. System.Object, ByVal e As System.EventArgs) Handles
  6. btnEmployeeCount.Click
  7. 'create connection to db
  8. Dim cn As New OracleConnection("Data Source=xe; _
  9. User Id=scott;Password=tiger")
  10. Try
  11. 'create the command object
  12. Dim cmd As New OracleCommand("SELECT COUNT(*) _
  13. FROM emp", cn)
  14. 'open the connection from command
  15. cmd.Connection.Open()
  16. 'execute the command and get the single value
  17. 'result
  18. Dim result As String = cmd.ExecuteScalar
  19. 'clear the resources
  20. cmd.Connection.Close()
  21. cmd.Dispose()
  22. 'display the output
  23. MessageBox.Show("No. of Employees: " & result)
  24. Catch ex As Exception
  25. 'display if any error occurs
  26. MessageBox.Show("Error: " & ex.Message)
  27. 'close the connection if it is still open
  28. If cn.State = ConnectionState.Open Then
  29. cn.Close()
  30. End If
  31. End Try
  32. End Sub
  33. End Class
 

The highlighted line in the above code simply executes the SELECT command, which retrieves the number of rows from the emp table and assigns this value to the result variable.


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape