I am looking to use SqlKata for a project. However, part of the project criteria is that queries should be able to be carried out as transactions. Is there a way I can perform a query or multiple queries with MSSQL transactions?
Many thanks.
I am looking to use SqlKata for a project. However, part of the project criteria is that queries should be able to be carried out as transactions. Is there a way I can perform a query or multiple queries with MSSQL transactions?
Many thanks.
SQLKata uses Dapper
as part of the execution of queries. Since Dapper supports transactionScopes, you can wrap your SQL Kata queries in transactions as well
using System.Transactions;
....
using (var scope = _db.Connection.BeginTransaction())
{
_db.Query("Posts").WhereNull("AuthorId").AsUpdate(new {
AuthorId = 10
});
...
scope.Commit();
}
currently, there is no direct support for transactions in Sqlkata, it's planned in the near future,
for now, you can invoke transactions directly using the db.Statement()
method.
db.Statement("BEGIN TRANSACTION");
db.Query("Transactions").Where(...).Update(new {Amount = 100});
db.Query("...").Delete();
if(err) {
db.Statement("ROLLBACK TRANSACTION");
}
db.Statement("COMMIT TRANSACTION");