1. 程式人生 > >.net 三層構架下使用事務更新多表資料

.net 三層構架下使用事務更新多表資料

 
//使用TransactionScope ,需要引用System.Transactions.dll
using (TransactionScope scope = new TransactionScope())//使用using,用完系統會自動回收資源
            {
                try
                {
                    M A = new M("a");
                    JsonObject a = new JsonObject();
                    a["a"] = 1;
                    a["b"] = "text1";
                    A.Add(a);
                    a = new JsonObject();
                    a["a"] = "text1";//a欄位的資料型別是int,所以此處會報錯.
                    a["b"] = "text1";
                    A.Add(a);
                    scope.Complete();//提交資料
                }
                catch(Exception ex)
                {
                    Common.Constant.AjaxShow("新增資料出錯了!");//出錯時,資料會自動回滾.
                }
            }