Asp.NET笔记(三)--用linq实现三层架构的简单单表全查询

用linq实现数据的简单全查询
一、在Model层添加linq to Sql类
二、在DAL层实现数据的全查询
如:

   public class Comments_DAL
    {
        /// <summary>
        /// 查询所有留言信息
        /// </summary>
        /// <returns></returns>
        public List<Comments> SelectAll()
        {
            DataCommonDataContext db = new DataCommonDataContext();

           var result = from m in db.Comments  //这里需要引入程序集system.data.linq
                        select m;
            List<Comments> list = result.ToList();
            return list;
        }
  }

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

   public class Comments_BLL
    {
        //实例化DAL层的对应类
        Comments_DAL comments_DAL = new Comments_DAL();
        /// <summary>
        /// 查询所有留言信息
        /// </summary>
        /// <returns></returns>
        public List<Comments> SelectAll()
        {
            return comments_DAL.SelectAll();
        }
    }

四、在UI层实现对BLL方法的调用和数据的展示
如:

  在网页后台代码中

        //实例化BLL类对象
        Comments_BLL comments_BLL = new Comments_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断页面是非回发页面
            if (!Page.IsPostBack)
            {
                List<Comments> lists = comments_BLL.SelectAll();
                this.GridView1.DataSource = lists;
                this.GridView1.DataBind();
            }

        }

 前台添加GridView1控件即可展示Comments表中的所有数据

原文地址:https://www.cnblogs.com/JuneDream/p/14147783.html