codecamp

事务

描述

Dapper支持事务和范围事务。

事务

从连接开始一个新事务,并将其传递给事务可选参数。

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();
    }
}

范围事务

在开始连接之前开始一个新的范围事务。

// 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();
}


缓冲
存储过程
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }