Skip to main content
All docs
V26.1
  • .NET Framework 4.6.2+

    Session.ExecuteQueryAsync(String, Object[], CancellationToken) 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.

    Asynchronously executes the specified SQL query and returns a result set.

    Namespace: DevExpress.Xpo

    Assembly: DevExpress.Xpo.v26.1.dll

    Declaration

    public Task<SelectedData> ExecuteQueryAsync(
        string sql,
        object[] parameterValues,
        CancellationToken cancellationToken = default(CancellationToken)
    )

    Parameters

    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.

    Optional Parameters

    Name Type Default Description
    cancellationToken CancellationToken null

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

    Returns

    Type Description
    Task<SelectedData>

    A Task<TResult> that returns a result set compatible with XPDataView.

    Remarks

    Use ExecuteQueryAsync to asynchronously query data stores for result sets. You can visualize result sets via the XPDataView. To learn how to access the resulting data, refer to How to: Access Data in SQL Query Results.

    The example below 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();
    SelectedData data = await GetEmployeesSimpleDataAsync(cts.Token);
    // ...
    public async Task<SelectedData> GetEmployeesSimpleDataAsync(CancellationToken cancellationToken) {
        const string queryString = 
            @"SELECT EmployeeID, (FirstName + ' ' + LastName) as Name, City, Country 
            FROM [Northwind].[dbo].[Employees] 
            WHERE City = @p1";
        return await session.ExecuteQueryAsync(queryString,
           new object[] { "London" },
           cancellationToken);
    }
    

    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.

    Note

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

    See Also