//数据库上下文对象 var dc = new DataContexts(); if (dc.Connection.State == ConnectionState.Closed) { dc.Connection.Open(); } using (var dcTran = dc.Connection.BeginTransaction()) { dc.Transaction = dcTran; try { dc.SubmitChanges(); dcTran.Commit(); // 提交事务 } catch (Exception ex) { dcTran.Rollback(); // 回滚事务 return rp.setError(ex.Message + ex.StackTrace); } }