private void btnAdd_Click(object sender, EventArgs e) { DbConnection conn = null; DbTransaction trans = null; try { string strText = ""; if (string.IsNullOrEmpty(txtAssName.Text)) { throw new Exception("请输入会员姓名"); } using (AMSEntities amsContext = new AMSEntities()) { conn = amsContext.Connection; if (conn.State != ConnectionState.Open) { conn.Open(); } using (trans = conn.BeginTransaction(IsolationLevel.ReadCommitted)) { //事务数据操作或其它一致性操作 tbBusiLog busiLog = new tbBusiLog(); busiLog.dtOperDate = DateTime.Now; busiLog.vcOperName = GlobalParams.oper.vcOperName; busiLog.vcOperID = GlobalParams.oper.vcOperID; switch (OperType) { case "ADD": tbAssociator assn = GetAssociator(); amsContext.AddTotbAssociator(assn); busiLog.iAssID = assn.iAssID; busiLog.vcAssName = assn.vcAssName; busiLog.vcOperType = "OT007"; amsContext.AddTotbBusiLog(busiLog); Helper.Save(amsContext); strText = "会员添加成功"; break; case "MODIFY": tbAssociator assm = amsContext.tbAssociator.FirstOrDefault(a => a.iAssID == ass.iAssID); if (assm == null) { throw new Exception("未找到会员"); } assm = GetAssociator(assm); busiLog.iAssID = assm.iAssID; busiLog.vcAssName = assm.vcAssName; busiLog.vcOperType = "OT008"; amsContext.AddTotbBusiLog(busiLog); Helper.Save(amsContext); strText = "会员修改成功"; break; } trans.Commit(); } } Helper.ShowInfo(this, strText); } catch (Exception ex) { //异常操作 ErrorLog.Write(this, ex); } finally { if (conn != null) { conn.Close(); } } }