Asp.NET笔记(八)--使用linq+三层架构实现数据的修改

一、在同页面修改个别已知字段的值

  1、DAL层中  

     /// <summary>
        /// 处理缺陷
        /// </summary>
        /// <param name="id">缺陷id</param>
        /// <returns>是否处理成功</returns>
        public bool ChuliDefect(int id)
        {
            //通过id找到缺陷信息
            Defect defect = (from i in db.Defect
                            where i.DefectID == id
                            select i).FirstOrDefault();
            //修改缺陷信息
            defect.DefectState = 2;//2为已处理
            defect.DealTime = DateTime.Now; //处理事件为当前时间
            //将保持提交给数据库
            db.SubmitChanges();
            return true;
        }    

说明:因为修改字段是固定的,所以我们只需要通过id找到要修改的实体字段,即可直接修改数据  

2、BLL层中

 /// <summary>
        /// 处理缺陷
        /// </summary>
        /// <param name="id">缺陷id</param>
        /// <returns>是否处理成功</returns>
        public bool ChuliDefect(int id)
        {
            return dAL.ChuliDefect(id);
        }

  3、UI层中

 前端显示页面

    <asp:Button ID="Button1" runat="server" Text="处理缺陷" Visible='<%#Eval("DefectState").ToString()=="1"?true:false %>' CommandName="ChuLi" CommandArgument='<%#Eval("DefectID") %>' />

 后台RowCommand事件中

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChuLi")
            {
                //获取缺陷ID
                int id = int.Parse( e.CommandArgument.ToString());
                if (bll.ChuliDefect(id))
                {
                    Response.Write("<script>alert('处理成功!')</script>");
          
//重新加载GridView实现刷新
            List<DeviceDefect> list = bll.SelectDeviceDefect();

this.GridView1.DataSource = list;
this.GridView1.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('处理失败!')</script>");
                }
            }
        }

-----------------------------------------------------------------------------------------------------------------------------------------------

二、需要跳转页面,由用户输入要修改的值进行修改

 如:

  第一步:实现跳转修改页面,并且在页面中加载出要修改的数据

  所以在跳转时必须告诉页面,为要修改的是哪一条数据,用参数传递的方法把id传值给新页面

     跳转方法一:直接在页面跳转

  <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl='<%# "~/AddFurnitureWeb.aspx?id="+Eval("Id") %>'>编辑</asp:LinkButton>

    这种拼接模式容易出错,导致整个控件创建错误,但是简明直观

    跳转方法二:进入到后台代码跳转

  前台: <asp:LinkButton ID="LinkButton2" runat="server" CommandName="edit" CommandArgument='<%#Eval("Id") %>'>编辑</asp:LinkButton>

  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "edit")//判断页面中设置的名称是否edit
            {
                string id = e.CommandArgument.ToString();//获取页面中的传值id
                Response.Redirect("AddFurnitureWeb.aspx?id=" + id); //拼接url参数
            }
        }

    这种拼接模式不易出错但代码量大,大家酌情选择,目的都是将id拼接到URL中进行数据传递

第二步:在修改页面加载事件中,加载页面需要呈现的数据  

 WarehouseBLL bll = new WarehouseBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //准备工作:绑定DropDownList1数据
                this.DropDownList1.DataSource = bll.SelectWarehouse();
                this.DropDownList1.DataTextField = "WarehouseName";
                this.DropDownList1.DataValueField = "WarehouseId";
                this.DropDownList1.DataBind();


                //获取URL参数传递的id值
                int id = int.Parse(Request.QueryString["Fid"]);

                //通过id查询要修改的实体对象
                List<Furniture> list = bll.SelectFurniture();
                Furniture f = list.Where(m => m.Id == id).FirstOrDefault();

                //将实体对象的值赋值给页面
                txtFName.Text = f.FurnitureName; //名称
                txtPinPai.Text = f.Brand; //品牌
                txtSalNum.Text = f.SoldCount.ToString();//销售数量
                txtTotalCount.Text = f.TotalCount.ToString();//总数量
                DropDownList1.SelectedValue = f.WarehouseId.ToString(); //所属仓库
            }

第三步:实现修改功能

  分别在DAL,BLL和UI层写传递数据

  DAL层:

 /// <summary>
        /// 修改家具信息
        /// </summary>
        /// <param name="newFurn">修改后的实体对象</param>
        /// <returns>是否修改成功</returns>
        public bool EditFurniture(Furniture newFurn)
        {
            //实例化上下文数据
            DataFurnitureManagementDataContext db = new DataFurnitureManagementDataContext();

            //因为修改的时候不修改主键值,所以可以用个新对象的主键id查询到表中要修改的那个具体对象
            Furniture furniture = (from i in db.Furniture
                                   where i.Id == newFurn.Id
                                   select i).FirstOrDefault();
            //判断实体对象值是否为空
            if (furniture != null)
            {
                //将修改后的实体对象值一一赋值给查询到的对象
                furniture.FurnitureName = newFurn.FurnitureName;
                furniture.Brand = newFurn.Brand;
                furniture.SoldCount = newFurn.SoldCount;
                furniture.TotalCount = newFurn.TotalCount;
                furniture.WarehouseId = newFurn.WarehouseId;
                //注意,这个不要写
                //furniture.AddTime = newFurn.AddTime; //新实体中没有赋值的字段,这里不用添加,添加后会报错
                //将结果保存到数据库(一定要保存)
                db.SubmitChanges();

                return true;
            }
            else
            {
                return false;
            }
        }

BLL层中:

     /// <summary>
        /// 修改家具信息
        /// </summary>
        /// <param name="newFurn">修改后的实体对象</param>
        /// <returns>是否修改成功</returns>
        public bool EditFurniture(Furniture newFurn)
        {
            //实例化DAL
            WarehouseDAL dAL = new WarehouseDAL();
            return dAL.EditFurniture(newFurn);
        }

UI层中:

 //修改按钮
        protected void Button1_Click(object sender, EventArgs e)
        {
            //创建一个新的实体(修改后的实体),并对新实体赋值
            Furniture newFurn = new Furniture();
            newFurn.Id = int.Parse(Request.QueryString["Fid"]);  //ID一定要赋值,不然dal层找不到要修改的对象
            newFurn.FurnitureName = txtFName.Text; 
            newFurn.Brand = txtPinPai.Text;
            newFurn.SoldCount = int.Parse(txtSalNum.Text);
            newFurn.TotalCount = int.Parse(txtTotalCount.Text);
            newFurn.WarehouseId = int.Parse(this.DropDownList1.SelectedValue);

            //调用修改方法将新创建的实体对象传值给dal层进行修改
            if (bll.EditFurniture(newFurn))
            {
                Response.Write("<script>alert('修改成功!')</script>");
            }
            else
            {
                Response.Write("<Script>alert('修改失败!')</Script>");
            }
        }

这样就可以实现修改功能啦~

--------------------------------------------------------------------------------

tips:对比两种修改大家可以发现其实DAL层的写法是非常类似的,只不过后面修改整个实体对象时我们需要修改的字段过多,用实体对象传递为们要修改的参数,会事半功倍!

 

posted @ 2020-12-19 09:16  JuneDream  阅读(425)  评论(1编辑  收藏  举报