Table of Contents

Method ExecuteReaderAsync

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.MSSqlClient.dll

ExecuteReaderAsync(SqlCommand, Func<SqlDataReader, ValueTask>, CommandBehavior, CancellationToken)

Asynchronously executes the command and passes the SqlDataReader to the handler. The reader is disposed afterwards.

public static ValueTask ExecuteReaderAsync(this SqlCommand command, Func<SqlDataReader, ValueTask> handler, CommandBehavior behavior = CommandBehavior.Default, CancellationToken cancellationToken = default)

Parameters

command SqlCommand

The command to execute. Its connection is opened if needed and closed again if it was opened here; neither the command nor the connection is disposed.

handler Func<SqlDataReader, ValueTask>

The function that reads from the reader. Do not let the reader escape it.

behavior CommandBehavior

The behavior to use with the data reader. CloseConnection is added when the command opens a closed connection.

cancellationToken CancellationToken

A token that can cancel opening the connection and executing the command.

Returns

ValueTask

A task that completes after handler has completed and the reader has been disposed.

Remarks

Exceptions from the provider (for example SqlException) propagate unchanged. The reader is disposed, and with it a connection that this method opened, even when the handler throws.

Exceptions

ArgumentNullException

command or handler is null. The exception is reported through the returned task.

OperationCanceledException

The operation was canceled.

ExecuteReaderAsync(SqlCommand, Action<SqlDataReader>, CommandBehavior, CancellationToken)

Asynchronously executes the command and passes the SqlDataReader to the handler. The reader is disposed afterwards.

public static ValueTask ExecuteReaderAsync(this SqlCommand command, Action<SqlDataReader> handler, CommandBehavior behavior = CommandBehavior.Default, CancellationToken cancellationToken = default)

Parameters

command SqlCommand

The command to execute. Its connection is opened if needed and closed again if it was opened here; neither the command nor the connection is disposed.

handler Action<SqlDataReader>

The function that reads from the reader. Do not let the reader escape it.

behavior CommandBehavior

The behavior to use with the data reader. CloseConnection is added when the command opens a closed connection.

cancellationToken CancellationToken

A token that can cancel opening the connection and executing the command.

Returns

ValueTask

A task that completes after handler has completed and the reader has been disposed.

Remarks

Exceptions from the provider (for example SqlException) propagate unchanged. The reader is disposed, and with it a connection that this method opened, even when the handler throws.

Exceptions

ArgumentNullException

command or handler is null. The exception is reported through the returned task.

OperationCanceledException

The operation was canceled.

ExecuteReaderAsync<T>(SqlCommand, Func<SqlDataReader, T>, CommandBehavior, CancellationToken)

Asynchronously executes the command and passes the SqlDataReader to the transform function, returning its result. The reader is disposed afterwards.

public static ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command, Func<SqlDataReader, T> transform, CommandBehavior behavior = CommandBehavior.Default, CancellationToken cancellationToken = default)

Parameters

command SqlCommand

The command to execute. Its connection is opened if needed and closed again if it was opened here; neither the command nor the connection is disposed.

transform Func<SqlDataReader, T>

The function that reads from the reader and produces the result. Do not let the reader escape it.

behavior CommandBehavior

The behavior to use with the data reader. CloseConnection is added when the command opens a closed connection.

cancellationToken CancellationToken

A token that can cancel opening the connection and executing the command.

Returns

ValueTask<T>

A task that produces the value returned by transform.

Type Parameters

T

The type returned by the transform function.

Remarks

Exceptions from the provider (for example SqlException) propagate unchanged. The reader is disposed, and with it a connection that this method opened, even when the transform function throws.

Exceptions

ArgumentNullException

command or transform is null. The exception is reported through the returned task.

OperationCanceledException

The operation was canceled.

ExecuteReaderAsync<T>(SqlCommand, Func<SqlDataReader, ValueTask<T>>, CommandBehavior, CancellationToken)

Asynchronously executes the command and passes the SqlDataReader to the transform function, returning its result. The reader is disposed afterwards.

public static ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command, Func<SqlDataReader, ValueTask<T>> transform, CommandBehavior behavior = CommandBehavior.Default, CancellationToken cancellationToken = default)

Parameters

command SqlCommand

The command to execute. Its connection is opened if needed and closed again if it was opened here; neither the command nor the connection is disposed.

transform Func<SqlDataReader, ValueTask<T>>

The function that reads from the reader and produces the result. Do not let the reader escape it.

behavior CommandBehavior

The behavior to use with the data reader. CloseConnection is added when the command opens a closed connection.

cancellationToken CancellationToken

A token that can cancel opening the connection and executing the command.

Returns

ValueTask<T>

A task that produces the value returned by transform.

Type Parameters

T

The type returned by the transform function.

Remarks

Exceptions from the provider (for example SqlException) propagate unchanged. The reader is disposed, and with it a connection that this method opened, even when the transform function throws.

Exceptions

ArgumentNullException

command or transform is null. The exception is reported through the returned task.

OperationCanceledException

The operation was canceled.