private void btnAdd_Click(object sender, EventArgs e) { try { if (txtOperID.Text.Trim().Length == 0) { throw new Exception("请输入操作员帐号"); } if (txtOperName.Text.Trim().Length == 0) { throw new Exception("请输入操作员名称"); } if (txtOperPwd.Text.Trim().Length == 0) { throw new Exception("请输入密码"); } if (txtOperPwdConfirm.Text.Trim().Length == 0) { throw new Exception("请输入密码确认"); } if (!txtOperPwd.Text.Equals(txtOperPwdConfirm.Text)) { throw new Exception("确认密码不一致"); } using (AMSEntities amsContext = new AMSEntities()) { switch (btnAdd.Text) { case "添加": tbOper eOper = amsContext.tbOper.FirstOrDefault(o => o.vcOperID == txtOperID.Text || o.vcOperName == txtOperName.Text); if (eOper != null) { throw new Exception("相同名称或者(帐号的操作员已存在"); } tbOper nOper = new tbOper(); nOper.vcLimit = cmbRole.SelectedValue.ToString(); nOper.vcOperID = txtOperID.Text; nOper.vcOperLevel = cmbOperLevel.SelectedValue.ToString(); //"OL001"; nOper.vcOperName = txtOperName.Text; nOper.vcOperPwd = txtOperPwd.Text; nOper.iFlag = 0; amsContext.AddTotbOper(nOper); amsContext.SaveChanges(); MessageBox.Show(this, "操作员添加成功", "添加操作员"); break; case "修改": int i = amsContext.tbOper.Count(o => o.vcOperName == txtOperName.Text); if (i > 1) { throw new Exception("相同名称的操作员已存在"); } tbOper oper = amsContext.tbOper.FirstOrDefault(o => o.vcOperID == txtOperID.Text); oper.vcLimit = cmbRole.SelectedValue.ToString(); oper.vcOperName = txtOperName.Text; oper.vcOperPwd = txtOperPwd.Text; oper.vcOperLevel = cmbOperLevel.SelectedValue.ToString(); amsContext.SaveChanges(); MessageBox.Show(this, "操作员修改成功", "修改操作员"); break; } } frmOper_Load(null, null); } catch (Exception ex) { ErrorLog.Write(this, ex, "操作员"); } }