Dapper - Transaction
交易是如此的重要,Dapper當然也不會忘記。
//Transaction using (SqlConnection conn = new SqlConnection(strConnection)) { string strSql = " UPDATE Users SET col1=@c1 WHERE col2=@c2" ; dynamic datas = new []{ new { c1 = "A", c2 = "A2" } , new { c1 = "B", c2 = "B2" } , new { c1 = "C", c2 = "C2" }}; //交易 using(var tran = conn.BeginTransaction()) { conn.Execute( strSql, datas); tran.Commit(); } }
單一資料庫時建議使用(效能較好)。
//TransactionScope //加入參考 using System.Transactions; //交易 using(var tranScope = new TransactionScope()) { using (SqlConnection conn = new SqlConnection(strConnection)) { string strSql = " UPDATE Users SET col1=@c1 WHERE col2=@c2" ; dynamic datas = new []{ new { c1 = "A", c2 = "A2" } , new { c1 = "B", c2 = "B2" } , new { c1 = "C", c2 = "C2" }}; conn.Execute( strSql, datas); } tranScope.Complete(); }
用於異質資料庫交易。
参考:https://dotblogs.com.tw/OldNick/2018/01/15/Dapper#Dapper%20-%20Transaction
标签:事务,Transaction,using,c2,new,Dapper,c1,conn From: https://www.cnblogs.com/friend/p/16754184.html