.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

Session.ExecuteScalar(String, Object[]) Method

Executes the specified SQL query and returns the first column of the first row in the result set returned by the query.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v21.1.dll


public object ExecuteScalar(
    string sql,
    object[] parameterValues


Name Type Description
sql String

Specifies an SQL statement.

parameterValues Object[]

An array of objects specifying parameters to pass to the database server along with the query.


Type Description

The first column of the first row in the query’s result set. If the column is not found in the result set, a null reference (Nothing in Visual Basic) is returned. If the value in the database is null, DBNull.Value is returned.


Use ExecuteScalar to query data stores for a single or aggregate value, without having to retrieve a result set.

The following example demonstrates how to use this method. Here, session is the Session instance.

public int GetEmployeeOrdersCount(int employeeId) {
    return (int)session.ExecuteScalar(
        "SELECT COUNT(*) FROM [Northwind].[dbo].[Orders] WHERE [EmployeeID] = @p1", 
        new object[] { employeeId }

Note that a format of parameter names in a SQL statement depends on the ADO.NET provider and database you use.

To learn more about executing SQL queries in XPO, refer to Direct SQL Queries.


The ExecuteScalar method sends queries directly, so the correct query syntax depends on a particular database server.

See Also