Send feedback on this topic.
Teradata.Client.Provider
ExecuteScalarAsync(CancellationToken) Method
Example 



Teradata.Client.Provider Namespace > TdCommand Class > ExecuteScalarAsync Method : ExecuteScalarAsync(CancellationToken) Method
A Threading.CancellationToken used to indicate that the asynchronous operation should be canceled.
A Task-based asynchronous version of the TdCommand.ExecuteScalar method. Executes the SQL statement (CommandText property) and returns the first column of the first row. The cancellation token may be use to cancel the execution of the TdCommand.
Syntax
'Declaration
 
Public Overloads Overrides NotOverridable Function ExecuteScalarAsync( _
   ByVal cancellationToken As CancellationToken _
) As Task(Of Object)
'Usage
 
Dim instance As TdCommand
Dim cancellationToken As CancellationToken
Dim value As Task(Of Object)
 
value = instance.ExecuteScalarAsync(cancellationToken)
public override Task<object> ExecuteScalarAsync( 
   CancellationToken cancellationToken
)
public:
Task<Object^>^ ExecuteScalarAsync( 
   CancellationToken cancellationToken
) override 

Parameters

cancellationToken
A Threading.CancellationToken used to indicate that the asynchronous operation should be canceled.

Return Value

Returns a Task<object> representing the asynchronous operation.
Exceptions
ExceptionDescription
Can be thrown because of one of the following errors: 1) TdCommand.Connection is not in Open state or is null. 2) TdCommand.CommandText is null or empty. 3) TdCommand has an open DataReader. 4) TdCommand has an active request. 5) The local transaction associated with TdCommand is not the same as the local transaction associated with TdConnection. Assign the local transaction to the TdCommand.Transaction property.
The TdCommand is closed / disposed.
.NET Data Provider for Teradata detected an error.
Remarks

Executes the SQL statement and returns the first column of the first row.

When CommandType property is set to System.Data.CommandType.StoredProcedure, any output parameters are populated with data and a null object reference is returned.

If this function generates a runtime exception, that exception will be wrapped in an AggregateException within the returned Task:

Exception Description
IndexOutOfRangeException Can be thrown because of one of the following errors: 1) The TdParameter.Offset is outside of 0 through array size � 1 2) More in-out/out parameters have been specified than returned from the stored procedure.
InvalidCastException One or more parameters cannot be converted to the Advanced SQL Engine native types.
OperationCanceledException The asynchronous operation was canceled.
TdException The Advanced SQL Engine returned an error, or .NET Data Provider for Teradata detected an error.
Example
The following example creates a TdConnection object and executes a SQL statement.
public Object ExecuteQuery(string connectionString, string commandText)
{
    Object scalar;
    
    using(TdConnection cn = new TdConnection(connectionString))
    {
        cn.Open();
            
        TdCommand cmd = new TdCommand(commandText, cn);
        CancellationTokenSource cts = new CancellationTokenSource();
        
        // Cancel the command if it doesn't complete in one minute
        cts.CancelAFter(60000);
        
        Task<object> task = cmd.ExecuteScalarAsync(cts.Token);
        
        // perform other tasks
        
        // wait for the result
        scalar = task.Result;
    }
            
    return scalar;
}
Requirements

Target Platforms: Windows 8.1, Windows 10, Windows Server 2012 R2, Windows Server 2016, Windows Server 2019

See Also

Reference

TdCommand Class
TdCommand Members
Overload List