DbCommand.ExecuteScalarAsync Method

Definition

Implements the asynchronous version of ExecuteScalar(), but returns a Task synchronously, blocking the calling thread.

Overloads

ExecuteScalarAsync()

An asynchronous version of ExecuteScalar(), which executes the command and returns the first column of the first row in the first returned result set. All other columns, rows and result sets are ignored.

Invokes ExecuteScalarAsync(CancellationToken) with CancellationToken.None.

ExecuteScalarAsync(CancellationToken)

This is the asynchronous version of ExecuteScalar(). Providers should override with an appropriate implementation. The cancellation token may optionally be ignored.

The default implementation invokes the synchronous ExecuteScalar() method and returns a completed task, blocking the calling thread. The default implementation will return a cancelled task if passed an already cancelled cancellation token. Exceptions thrown by ExecuteScalar will be communicated via the returned Task Exception property.

Do not invoke other methods and properties of the DbCommand object until the returned Task is complete.

Remarks

For more information about asynchronous programming, see Asynchronous Programming.

ExecuteScalarAsync()

Source:
DbCommand.cs
Source:
DbCommand.cs
Source:
DbCommand.cs

An asynchronous version of ExecuteScalar(), which executes the command and returns the first column of the first row in the first returned result set. All other columns, rows and result sets are ignored.

Invokes ExecuteScalarAsync(CancellationToken) with CancellationToken.None.

public System.Threading.Tasks.Task<object> ExecuteScalarAsync ();
public System.Threading.Tasks.Task<object?> ExecuteScalarAsync ();

Returns

A task representing the asynchronous operation.

Exceptions

An error occurred while executing the command.

Remarks

For more information about asynchronous programming, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by ExecuteScalar().

See also

Applies to

.NET 9 וגירסאות אחרות
מוצר גירסאות
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

ExecuteScalarAsync(CancellationToken)

Source:
DbCommand.cs
Source:
DbCommand.cs
Source:
DbCommand.cs

This is the asynchronous version of ExecuteScalar(). Providers should override with an appropriate implementation. The cancellation token may optionally be ignored.

The default implementation invokes the synchronous ExecuteScalar() method and returns a completed task, blocking the calling thread. The default implementation will return a cancelled task if passed an already cancelled cancellation token. Exceptions thrown by ExecuteScalar will be communicated via the returned Task Exception property.

Do not invoke other methods and properties of the DbCommand object until the returned Task is complete.

public virtual System.Threading.Tasks.Task<object> ExecuteScalarAsync (System.Threading.CancellationToken cancellationToken);
public virtual System.Threading.Tasks.Task<object?> ExecuteScalarAsync (System.Threading.CancellationToken cancellationToken);

Parameters

cancellationToken
CancellationToken

A token to cancel the asynchronous operation.

Returns

A task representing the asynchronous operation.

Exceptions

An error occurred while executing the command.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

For more information about asynchronous programming, see Asynchronous Programming.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by ExecuteScalar().

See also

Applies to

.NET 9 וגירסאות אחרות
מוצר גירסאות
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0