12. Understanding .NET Data Provider Connectivity : IngresDataReader Object--Retrieve Data from the Database : ExecuteScalar Method--Obtain a Single Value from a Database
 
Share this page                  
ExecuteScalar Method--Obtain a Single Value from a Database
To return database information that is a single value rather than in the form of a table or data stream--for example, to return the result of an aggregate function such as Count(*), Sum(Price), or Avg(Quantity)--use the IngresCommand object's ExecuteScalar method. The ExecuteScalar method returns as a scalar value the value of the first column of the first row of the result set.
The following code example uses the Count aggregate function to return the number of records in a table:
Visual Basic:
Dim cmd As IngresCommand = New IngresCommand("SELECT Count(*) FROM Personnel", conn)
Dim count As Int32 = CInt(cmd.ExecuteScalar())
C#:
IngresCommand cmd = new IngresCommand("SELECT Count(*) FROM Personnel", conn);
Int32 count = (Int32)cmd.ExecuteScalar();