ISqlExecutor.ScalarAsync<TValue>(ISqlQueryBuilder sqlQueryBuilder, object[] parameters, CancellationToken cancellationToken) Method
.NET Standard 2.x
Asynchronously executes the SQL SELECT statement created by an ISqlQueryBuilder
object and returns the first column of the first row in the result set. All other columns and rows are ignored.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public Task<TValue> ScalarAsync<TValue>(ISqlQueryBuilder sqlQueryBuilder, object[] parameters, CancellationToken cancellationToken)
Type Parameters
TValue
The datatype of the first column in the result set.
Parameters
sqlQueryBuilder
SnapObjects.Data.ISqlQueryBuilder
A SqlQueryBuilder
object which can be used to generate a SQL SELECT statement.
parameters
System.Object[]
One or more objects which contain the values corresponding to the SQL parameter identifier names.
You can also pass the arguments directly in the order in which they appear in the SQL SELECT statement generated by sqlQueryBuilder
without using the objects.
cancellationToken
CancellationToken
A cancellation token that can be used by other objects or threads to receive notice of cancellation.
Returns
Task<TValue>
Returns a task that represents the asynchronous operation.
Remarks
You can use parameter placeholders in the raw SQL Statement.
It is recommended to use the syntax for parameter placeholders that is specific to the data source.
e.g., uses @parametername
for SQL Server and uses :parametername
for Oracle.
Examples
The following code example demonstrates how to use the ScalarAsync<TValue> (ISqlQueryBuilder, params object[], CancellationTokenSource)
method to execute the SQL statement generated by an ISqlQueryBuilder
object. It uses a CancellationTokenSource type parameter to make this operation cancelable.
using System;
using System.Threading;
using System.Threading.Tasks;
using SnapObjects.Data;
namespace Appeon.ApiDoc.ISqlExecutorExamples
{
public class ScalarAsyncExample
{
private readonly SchoolContext _context;
public ScalarAsyncExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public async Task<string> Example4(CancellationTokenSource cts)
{
// Creates a SqlQueryBuilder.
var builder = new SqlQueryBuilder();
var idParam = SqlBuilder.Parameter<int>("id");
// Builds the SQL SELECT statement.
builder.Select("Title")
.From("Course")
.Where("Credits", idParam);
int id = 2;
string title = null;
try
{
// If a task has been cancelled, the call to the
// SqlExecutor.ScalarAsync method throws an OperationCanceledException.
title = await _context.SqlExecutor.ScalarAsync<string>(builder,
new object[] { id }, cts.Token);
Console.WriteLine("Title: {0}", title);
}
catch (OperationCanceledException e)
{
// The operation was cancelled before completion.
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
cts.Dispose();
}
return title;
/*This code produces the following output:
A task was canceled.
*/
}
}
}
Applies to
.NET Standard
2.x