See Also

DbCommandBase Class  | DbCommandBase Members

Language

Visual Basic

C#

Show All

See AlsoLanguagesDevart.DataSend comments on this topic.

ExecuteScalar Method

Devart.Common Namespace > DbCommandBase Class : ExecuteScalar Method

Executes the query, and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.

[Visual Basic]
Overrides Public Function ExecuteScalar() As Object
[C#]
public override object ExecuteScalar();

Return Type

The first column of the first row in the result set.

Remarks

Use the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database. This requires less code than using the ExecuteReader method, and then performing the operations necessary to generate the single value from the data returned by a DbDataReader.

Example

This example shows typical usage of ExecuteScalar method

[C#] 

using(OracleConnection conn = new OracleConnection(connectionString)) { 
  conn.Open(); 
  OracleCommand cmd = conn.CreateCommand(); 
  cmd.CommandText = "SELECT count(*) as NumberOfRegions FROM regions"; 
  Int64 count = Convert.ToInt64(cmd.ExecuteScalar());  

[Visual Basic] 

Dim conn As New OracleConnection(connectionString)
conn.Open()
Dim cmd As OracleCommand = conn.CreateCommand()
cmd.CommandText = "SELECT count(*) as NumberOfRegions FROM regions"
Dim count As Int64 = Convert.ToInt64(cmd.ExecuteScalar());
conn.Close()

See Also

DbCommandBase Class  | DbCommandBase Members

 

 


© 2002 - 2013 Devart. All Rights Reserved.