• Asp.NET笔记(四)--使用linq+三层架构实现数据的添加


    一、在Model层添加Linq to Sql类
      在项目中添加新建项-->linq to Sql类-->服务资源管理器添加数据连接-->将数据库中表拖拽到linq类中-->保存
    二、在DAL层写实现添加的方法
      如:在DAL层中的Comments_DAL类中添加如下方法

            /// <summary>
            /// 添加评论
            /// </summary>
            /// <param name="comments">评论实体对象</param>
            /// <returns></returns>
            public bool AddComments(Comments comments)
            {
                //进行实例化
                DataCommonDataContext db = new DataCommonDataContext();
                //判断参数实体对象是否为空
                if (comments !=null)
                {
                    //将实体添加给Linq上下文对象中
                    db.Comments.InsertOnSubmit(comments);
                    //实现对数据库的更改
                    db.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }

    三、在BLL层实现DAL层方法的调用

    如:
    public class Comments_BLL
    {

            /// <summary>
            /// 添加评论
            /// </summary>
            /// <param name="comments">评论实体对象</param>
            /// <returns></returns>
            public bool AddComments(Comments comments)
            {
                return comments_DAL.AddComments(comments);
            }

    }

    四、在UI层实现对BLL方法的调用实现添加
    如:
      

         //实例化BLL类
            Comments_BLL bll = new Comments_BLL();
            protected void btnSubmitContent_Click(object sender, EventArgs e)
            {
                //实例化一个评论的实体对象
                Comments comments = new Comments();
                //接受页面文本框的值赋值给实体对象
                comments.UserName = txtUserName.Text;
                comments.Content = txtContent.Text;
              
                //调用bll相应的方法添加评论
                if (bll.AddComments(comments) == true)
                {              
                   Response.Write("<script>alert('添加成功!')</script>");
                }
                else
                {
                    Response.Write("<script>alert('添加失败!')</script>");
                }
            }

  • 相关阅读:
    WPF基础篇之静态资源和动态资源
    15-Node-数据库
    15-Node
    12-Git
    总-S04-03 项目-大事件
    00-PHP难点
    08-PHP基础
    15-ES6
    16-Vue-webpack
    00-Web难点
  • 原文地址:https://www.cnblogs.com/JuneDream/p/14147802.html
Copyright © 2020-2023  润新知