|
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
Imports%20Oracle.DataAccess.Client%20%0A%0APublic%20Class%20Form9%0A%20%0A%20%20%20%20%20Private%20Sub%20btnEmployeeCount_Click%28ByVal%20sender%20As%20%0A%20%20%20%20%20System.Object%2C%20ByVal%20e%20As%20System.EventArgs%29%20Handles%20%0A%20%20%20%20%20btnEmployeeCount.Click%20%0A%20%20%20%20%20%20%20%20%27create%20connection%20to%20db%20%0A%20%20%20%20%20%20%20%20Dim%20cn%20As%20New%20OracleConnection%28%22Data%20Source%3Dxe%3B%20_%20%0A%20%20%20%20%20%20%20%20%20%20User%20Id%3Dscott%3BPassword%3Dtiger%22%29%20%0A%20%20%20%20%20%20%20%20Try%20%0A%20%20%20%20%20%20%20%20%20%20%27create%20the%20command%20object%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20cmd%20As%20New%20OracleCommand%28%22SELECT%20COUNT%28%2A%29%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20FROM%20emp%22%2C%20cn%29%20%0A%20%20%20%20%20%20%20%20%20%20%27open%20the%20connection%20from%20command%20%0A%20%20%20%20%20%20%20%20%20%20cmd.Connection.Open%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%27execute%20the%20command%20and%20get%20the%20single%20value%20%0A%20%20%20%20%20%20%20%20%20%20%27result%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20result%20As%20String%20%3D%20cmd.ExecuteScalar%20%0A%20%20%20%20%20%20%20%20%20%20%27clear%20the%20resources%20%0A%20%20%20%20%20%20%20%20%20%20cmd.Connection.Close%28%29%20%0A%20%20%20%20%20%20%20%20%20%20cmd.Dispose%28%29%20%0A%20%20%20%20%20%20%20%20%20%20%27display%20the%20output%20%0A%20%20%20%20%20%20%20%20%20%20MessageBox.Show%28%22No.%20of%20Employees%3A%20%22%20%26amp%3B%20result%29%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%20%0A%20%20%20End%20Sub%20%0AEnd%20Class
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)
|