ISqlBuilderBase.ValidateAsync(DataContext context, bool throwError, CancellationToken cancellationToken = default) Method
.NET Standard 2.x
Asynchronously validates the raw SQL string for the specified database generated by the current sqlbuilder.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
Task<bool> ValidateAsync(DataContext context, bool throwError, CancellationToken cancellationToken = default);
Parameters
context
SnapObjects.Data.DataContext
A DataContext
object which contains the database connection information.
throwError
System.Boolean
Optional.
True
(default ) -- to throw an exception if the raw SQL is not valid in the specified database;
False
-- Not to throw an exception if the raw SQL is not valid in the specified database.
cancellationToken
CancellationToken
(Optional) A cancellation token that can be used by other objects or threads to receive notice of cancellation.
Returns
Task<bool>
Returns a task that represents the asynchronous operation.
Examples
The following code example demonstrates how to Asynchronous validate a SqlQueryBuilder
object. It uses a CancellationTokenSource type parameter to make this operation cancelable.
using SnapObjects.Data;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Appeon.ApiDoc.ISqlBuilderBaseExamples
{
public class ValidateAsyncExample
{
private SchoolContext _context;
public ValidateAsyncExample(SchoolContext dataContext)
{
// Sets Data Context
_context = dataContext;
}
public async Task<bool> Example2(CancellationTokenSource cts)
{
// Builds a SQL statement.
var sqlBuilder = new SqlQueryBuilder();
sqlBuilder.Select("name")
.From("Department")
.WhereValue("DepartmentId", 1);
bool valid = false;
try
{
// Asynchronous validates the SQL statement in database.
// If a task has been cancelled, the call to the ISqlModelMapper.LoadAllAsync
// method throws an OperationCanceledException.
valid = await sqlBuilder.ValidateAsync(_context, throwError: true, cts.Token);
Console.WriteLine("Validation Result (second time): {0}", valid);
}
catch (OperationCanceledException e)
{
// The operation was cancelled before completion.
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
cts.Dispose();
}
return valid;
/*This code produces the following output:
A task was canceled.
*/
}
}
}
Applies to
.NET Standard
2.x