天天看點

Linq中兩種更新操作

方法一: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

        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

        /// 根據工号修改手機号

        /// <param name="num">工号</param>

        /// <param name="newphone">新的手機号</param>

        public void modifycellphonebynum(string num, string newphone)

                var stu = (from s in dc.teacherbasicinformation

                           where s.teachernum == num

                           select s).firstordefault();

                stu.teachercellphone = newphone;

                dc.submitchanges();

            catch

            { }

bll

        public static void modifycellphonebynum(string num, string newphone)

            new teacherdal().modifycellphonebynum(num, newphone);

        ///  編輯中的更新

        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();

            }   

繼續閱讀