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

    Session.ExecuteNonQueryAsync(CancellationToken, String, String[], Object[]) 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 statement and returns the number of rows affected.

    Namespace: DevExpress.Xpo

    Assembly: DevExpress.Xpo.v26.1.dll

    Declaration

    public Task<int> ExecuteNonQueryAsync(
        CancellationToken cancellationToken,
        string sql,
        string[] parameterNames,
        object[] parameterValues
    )

    Parameters

    Name Type Description
    cancellationToken CancellationToken

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

    sql String

    Specifies an SQL statement.

    parameterNames String[]

    Specifies parameter names.

    parameterValues Object[]

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

    Returns

    Type Description
    Task<Int32>

    A Task<TResult> that returns a number of affected rows.

    Remarks

    Use ExecuteNonQueryAsync to asynchronously execute SQL statements that do not produce result sets, such as UPDATE, INSERT, and DELETE statements.

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

    using System.Threading;
    using DevExpress.Xpo;
    // ...
    CancellationTokenSource cts = new CancellationTokenSource();
    await UpdateOrderDetailsAsync(cts.Token);
    // ...
    public async Task UpdateOrderDetailsAsync(CancellationToken cancellationToken) {
        const string queryString = 
            "UPDATE [Northwind].[dbo].[Order Details] SET [Discount] = @p1 WHERE [UnitPrice] > @p2";
        int rowsAffected = await session.ExecuteNonQueryAsync(
            cancellationToken,
            queryString,
            new string[] { "p1", "p2" },
            new object[] { 0.15, 100 }
        );
    }
    

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

    For more information on executing SQL statements in XPO, refer to Direct SQL Queries.

    Note

    The ExecuteNonQueryAsync method sends statements directly, so the correct statement syntax depends on a particular database server.

    See Also