dotConnect Universal Documentation
Devart.Data.Universal Namespace / UniCommand Class / ExecuteScalar Method
Example

ExecuteScalar Method (UniCommand)
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.
Syntax
'Declaration
 
Public Overrides Function ExecuteScalar() As Object
 

Return Value

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 Devart.Common.DbCommand.ExecuteReader method, and then performing the operations necessary to generate the single value from the data returned by a Devart.Common.DbDataReader.
Example
This example shows typical usage of Devart.Common.DbCommand.ExecuteScalar() method.
using(UniConnection conn = new UniConnection(connectionString)) {
  conn.Open();
  UniCommand cmd = conn.CreateCommand();
  cmd.CommandText = "SELECT count(*) as NumberOfRegions FROM regions";
  Int64 count = Convert.ToInt64(cmd.ExecuteScalar()); 
}
Dim conn As New UniConnection(connectionString)
conn.Open()
Dim cmd As UniCommand = conn.CreateCommand()
cmd.CommandText = "SELECT count(*) as NumberOfRegions FROM regions"
Dim count As Int64 = Convert.ToInt64(cmd.ExecuteScalar()); 
conn.Close()
See Also