Reviews
ODP.NETODP.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:
- Imports Oracle.DataAccess.Client
- Public Class Form9
- Private Sub btnEmployeeCount_Click(ByVal sender As
- System.Object, ByVal e As System.EventArgs) Handles
- btnEmployeeCount.Click
- 'create connection to db
- Dim cn As New OracleConnection("Data Source=xe; _
- User Id=scott;Password=tiger")
- Try
- 'create the command object
- Dim cmd As New OracleCommand("SELECT COUNT(*) _
- FROM emp", cn)
- 'open the connection from command
- cmd.Connection.Open()
- 'execute the command and get the single value
- 'result
- Dim result As String = cmd.ExecuteScalar
- 'clear the resources
- cmd.Connection.Close()
- cmd.Dispose()
- 'display the output
- MessageBox.Show("No. of Employees: " & result)
- 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
- End Sub
- 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.
ODP.NET
- Getting Started with Oracle and ODP.NET
- ODP.NET - Fundamental ODP.NET Classes to Retrieve Data
- ODP.NET - Retrieving Data Using OracleDataReader
- ODP.NET - Retrieving Multiple Rows on to the Grid
- ODP.NET - Retrieving Typed Data
- ODP.NET - Filling a DataTable Using OracleDataReader
- ODP.NET Retrieving a Single Row of Information Using OracleDataAdapter
- ODP.NET - Retrieving a Single Row of Information Using OracleDataAdapter
- ODP.NET - Working with DataTableReader
- ODP.NET - Populating a Dataset with a Single Data Table
- ODP.NET - Populating a Dataset with Multiple Data Tables
- ODP.NET - Presenting Master-Detail Information Using a Dataset
- ODP.NET - OracleCommand Object
- ODP.NET - Handling Nulls when Executing with ExecuteScalar
- ODP.NET - Handling Nulls when Working with OracleDataReader
- ODP.NET - Working with Bind Variables together with OracleParameter
- ODP.NET - Working with OracleDataAdapter with OracleCommand
- ODP.NET - Techniques to Improve Performance while Retrieving Data







