Dapper支持事務(wù)和范圍事務(wù)。
從連接開始一個新事務(wù),并將其傳遞給事務(wù)可選參數(shù)。
var sql = "Invoice_Insert";
using (var connection = My.ConnectionFactory())
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
var affectedRows = connection.Execute(sql,
new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
commandType: CommandType.StoredProcedure,
transaction: transaction);
transaction.Commit();
}
}
在開始連接之前開始一個新的范圍事務(wù)。
// using System.Transactions;
using (var transaction = new TransactionScope())
{
var sql = "Invoice_Insert";
using (var connection = My.ConnectionFactory())
{
connection.Open();
var affectedRows = connection.Execute(sql,
new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
commandType: CommandType.StoredProcedure);
}
transaction.Complete();
}
更多建議: