Skip to main content
.NET 8.0+

Session.ExecuteScalar(String) Method

SECURITY-RELATED CONSIDERATIONS

This method executes a raw SQL query string. Always validate, sanitize, or parameterize externally supplied SQL query strings to prevent unauthorized access to sensitive information.

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.v25.2.dll

NuGet Package: DevExpress.Xpo

Declaration

public object ExecuteScalar(
    string sql
)

Parameters

Name Type Description
sql String

Specifies an SQL statement.

Returns

Type Description
Object

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.

Remarks

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

public int GetOrdersCount() {
    return (int)session.ExecuteScalar(
        "SELECT COUNT(*) FROM [Northwind].[dbo].[Orders]"
    );        
}

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

Note

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

See Also