Edytuj

Udostępnij za pośrednictwem


IRelationalStorage.ExecuteAsync Method

Definition

Executes a given statement. Especially intended to use with INSERT, UPDATE, DELETE or DDL queries.

public System.Threading.Tasks.Task<int> ExecuteAsync (string query, Action<System.Data.IDbCommand> parameterProvider, System.Threading.CancellationToken cancellationToken = default, System.Data.CommandBehavior commandBehavior = System.Data.CommandBehavior.Default);
abstract member ExecuteAsync : string * Action<System.Data.IDbCommand> * System.Threading.CancellationToken * System.Data.CommandBehavior -> System.Threading.Tasks.Task<int>
Public Function ExecuteAsync (query As String, parameterProvider As Action(Of IDbCommand), Optional cancellationToken As CancellationToken = Nothing, Optional commandBehavior As CommandBehavior = System.Data.CommandBehavior.Default) As Task(Of Integer)

Parameters

query
String

The query to execute.

parameterProvider
Action<IDbCommand>

Adds parameters to the query. Parameter names must match those defined in the query.

cancellationToken
CancellationToken

The cancellation token. Defaults to None.

commandBehavior
CommandBehavior

The command behavior that should be used. Defaults to Default.

Returns

Affected rows count.

Examples

This sample shows how to make a hand-tuned database call.

//In contract to reading, execute queries are simpler as they return only
//the affected rows count if it is available.
var query = ""IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'Test') CREATE TABLE Test(Id INT PRIMARY KEY IDENTITY(1, 1) NOT NULL);"
int affectedRowsCount = await storage.ExecuteAsync(query, command =>
{
    //There aren't parameters here, but they'd be added like when reading.
    //As the affected rows count is the only thing returned, there isn't
    //facilities to read anything.
}).ConfigureAwait(continueOnCapturedContext: false);                

Applies to