SqlKata with cancellation token

431 Views Asked by At

I have some long-running queries that I'd like to cancel mid execution if the API request is canceled by the browser. I'm using SqlKata to execute my queries and I don't see a param for cancellation tokens. How would I be able to have my query canceled?

I would want something like this query.GetAsync<T>(cancelationToken: cancelationToken)

1

There are 1 best solutions below

2
On BEST ANSWER

Looks like the latest version of sql kata has it as one of the params. I just upgraded packages SqlKata and SqlKata.Execution to version 2.3.3 and I see it in the source code.

Alternatively, you can manually run it via Dapper by getting the SQL string and bindings from SqlKata and passing it into Dapper. I didn't test this but it would be something like this:

var connection = new SqlConnection(connectionString);
var compiler = new SqlServerCompiler();
var db = new QueryFactory(connection, compiler);
var query = db.Query("TableName"); // write your query here

var sqlResult = db.Compiler.Compile(query);
var sqlString = sqlResult.Sql;
var bindings = sqlResult.Bindings;

await using var connection = new SqlConnection(connectionString);
var result = await connection.QueryAsync(new CommandDefinition(sqlString, bindings, cancellationToken: cancellationToken));