The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.
All docs
V19.2
19.2
19.1
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

Session.ExecuteScalarAsync(String, CancellationToken) 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.v19.2.dll

Declaration

public Task<object> ExecuteScalarAsync(
    string sql,
    CancellationToken cancellationToken = default(CancellationToken)
)
Public Function ExecuteScalarAsync(
    sql As String,
    cancellationToken As CancellationToken = Nothing
) As Task(Of Object)
public Task<object> ExecuteScalarAsync(
    string sql,
    CancellationToken cancellationToken = default(CancellationToken)
)
Public Function ExecuteScalarAsync(
    sql As String,
    cancellationToken As CancellationToken = Nothing
) As Task(Of Object)

Parameters

Name Type Description
sql String

A String value that specifies a parameterized SQL query.

cancellationToken CancellationToken

A CancellationToken object that delivers a cancellation notice to the running operation.

Parameters

Name Type Description
sql String

A String value that specifies a parameterized SQL query.

cancellationToken CancellationToken

A CancellationToken object that delivers a cancellation notice to the running operation.

Returns

Type Description
Task<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.

Returns

Type Description
Task<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 ExecuteScalarAsync 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.

using System.Threading;
using System.Threading.Tasks;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
// ...
CancellationTokenSource cts = new CancellationTokenSource();
int ordersCount = await GetOrdersCountAsync(cts.Token);
// ...
public async Task<int> GetOrdersCountAsync(CancellationToken cancellationToken) {
    const string queryString = 
        "SELECT COUNT(*) FROM [Northwind].[dbo].[Orders]";
    object result = await session.ExecuteScalarAsync(
        queryString, 
        cancellationToken
    );
    return (int)result;
}

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

Note

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

See Also