A Task-based asynchronous version of the
TdCommand.ExecuteNonQuery method. The cancellation token may be use to cancel the execution of the
TdCommand
.
Parameters
- cancellationToken
Return Value
Returns a Task<Int32> with the number of rows affected (Activity Count). The number of
rows affected is only returned for DELETE, INSERT, and UPDATE statements.
For all other statements, the activity count that is returned will be -1.
The following example creates a TdConnection and asynchronously executes a SQL statement. See the example for
TdCommand.ExecuteNonQuery about handling the activity count overflow.
public int ExecuteQueryAsync(string connectionString, string commandText)
{
// Open a session to Teradata
using (TdConnection cn = new TdConnection(connectionString))
{
cn.Open();
// Execute the command
using (TdCommand cmd = new TdCommand(commandText, cn))
{
CancellationTokenSource cts = new CancellationTokenSource();
// Cancel the command if it doesn't complete in 20 seconds
cts.CancelAfter(20000);
Task<Int32> task;
try
{
task = cmd.ExecuteNonQueryAsync(cts.Token);
}
catch (Exception e)
{
// handle programming errors
}
// perform other tasks
// wait for the Task to complete
try
{
int activityCount = task.Result;
// Return the activity count
return activityCount;
}
catch (AggregateException ae)
{
// handle task execution errors
}
}
}
}
Target Platforms: Windows 8.1, Windows 10, Windows Server 2012 R2, Windows Server 2016, Windows Server 2019