方法一:Attach方法
EmployeeDAL
/// <summary> /// 更新雇员信息 /// </summary> /// <param name="e"></param> public void UpdateEmploee(Employee e) { try { dc.Employee.Attach(e); dc.Refresh(RefreshMode.KeepCurrentValues, e); dc.SubmitChanges(ConflictMode.ContinueOnConflict); } catch(Exception ex) { throw ex; } } ThreeTierBLL /// <summary> /// 更新雇员信息 /// </summary> /// <param name="e"></param> public static void UpdateEmploee(Employee e) { new EmployeeDAL().UpdateEmploee(e); } Web protected void btnSubmit_Click(object sender, EventArgs e) { Employee emp = new Employee(); emp.EmployeeID = this.txtEmpID.Text; emp.EmployeeName = this.txtEmpName.Text; emp.EmployeePhone = this.txtEmpPhone.Text; emp.DepartmentID = Convert.ToInt32(this.txtDepID.Text); EmployeeBLL.UpdateEmploee(emp); }
方法二:读出现有字段,更改
DAL
/// <summary> /// 根据工号修改手机号 /// </summary> /// <param name="num">工号</param> /// <param name="newphone">新的手机号</param> public void ModifyCellPhoneByNum(string num, string newphone) { try { var stu = (from s in dc.TeacherBasicInformation where s.TeacherNum == num select s).FirstOrDefault(); stu.TeacherCellPhone = newphone; dc.SubmitChanges(); } catch { } } BLL /// <summary> /// 根据工号修改手机号 /// </summary> /// <param name="num">工号</param> /// <param name="newphone">新的手机号</param> public static void ModifyCellPhoneByNum(string num, string newphone) { new TeacherDAL().ModifyCellPhoneByNum(num, newphone); } web /// <summary> /// 编辑中的更新 /// </summary> protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { if (((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim() == "") { PageExtension.Alert(this, "新的手机号不能为空!"); } else { TeacherBLL.ModifyCellPhoneByNum(GridView1.DataKeys[e.RowIndex].Value.ToString(), ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim()); this.GridView1.EditIndex = -1; Bind(); }
}