栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

EF实现多表连接查询

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

EF实现多表连接查询

  1. 建立数据库(BOOK)
  2. 建立数据表(Articles,Category) 

 表 Articles

字段名类型约束
IDint主键 自增 非空
Titlenvarchar(50)非空
Authornvarchar(50)非空
PushDatedatetime获取当前时间
Conentnvarchar(200)非空
Typeint外键约束ID

表 Category

字段名类型约束
IDint主键 自增 非空
Namenvarchar(50)非空

3. 创建Web窗体和类库

实体层 ASP.NET Web

数据访问层 类库DAL

业务逻辑层 类库BLL

模型层 类库Model 使用实体数据模型创建

 将Model类的App.Config中的以下代码


		
	

剪切并复制到Web窗体中的Web.config,主要目的是为了在Web窗体找到BOOKEntities。

将Model类中的程序集EntityFramework、程序集EntityFramework.SqlServer引用到Web窗体和DAL类中。

4.编写DAL,添加ArticlesDAO类和CategoryDAO类

ArticlesDAO类

//引用
using Model;

public class ArticlesDAO
    {
        //实例化BOOKEntities
        BOOKEntities db = new BOOKEntities();
        /// 
        /// 多表查询
        /// 
        /// 
        public object GetArticles()
        {
            var result = (from a in db.Articles
                         join c in db.Category on a.Type equals c.ID
                         select new { a.Title, a.Author, a.PushDate, a.Conent, Type= c.Name }).ToList();
            return result;
        }
        /// 
        /// 添加
        /// 
        /// 
        /// 
        public int Add(Articles articles)
        {
            db.Articles.Add(articles);
            return db.SaveChanges();
        }
    }

CategoryDAO类

//引用
using Model;

public  class CategoryDAO
    {
        //实例化
        BOOKEntities db = new BOOKEntities();
        
        /// 
        /// 查询所有
        /// 
        /// 
        public object GetCategory()
        {
            var result = (from c in db.Category
                         select c).ToList();
            return result;
        }
    }

5.编写BLL层,添加ArticlesService类和CategoryService类

ArticlesService类

//引用
using DAL;
using Model;

public class ArticlesService
    {
        ArticlesDAO articlesDAO = new ArticlesDAO();
        /// 
        /// 查询
        /// 
        /// 
        public object GetArticles()
        {
            return articlesDAO.GetArticles();
        }
        /// 
        /// 添加
        /// 
        /// 
        /// 
        public int Add(Articles articles)
        {
            return articlesDAO.Add(articles);
        }
    }

CategoryService类

//引用
using DAL;
using Model;

 public  class CategoryService
    {
        CategoryDAO categoryDAO = new CategoryDAO();
        /// 
        /// 查询
        /// 
        /// 
        public object GetCategory()
        {
            return categoryDAO.GetCategory();
        }
    }

6.添加Web窗体

Defult.aspx和Add.aspx

使用GridView绑定数据源 选择对象类型

使用LinkButton控件 进行跳转到添加页面

Defult.aspx窗体

        
            添加
            
            
            
        

 

Add.aspx窗体

 

        
            标题:
作者:
内容:
//下拉框控件进行绑定类别 类别:

Add.aspx.cs编写代码

//引用
using BLL;
using Model;

    public partial class Add : System.Web.UI.Page
    {
        //实例化ArticlesService,CategoryService
        ArticlesService articlesService = new ArticlesService();
        CategoryService categoryService = new CategoryService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //DropDownList绑定Category里的类别值 调用GetCategory方法
                this.dlType.DataSource = categoryService.GetCategory();
                this.dlType.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Articles articles = new Articles();
            articles.Title = txtTitle.Text;
            articles.Author = txtAuthor.Text;
            //获取当前时间
            articles.PushDate = DateTime.Now;
            articles.Conent = txtConent.Text;
            articles.Type =int.Parse( dlType.SelectedValue);
            int count = articlesService.Add(articles);
            if (count>0)
            {
                Response.Write(" ");
            }
        }
    }

根据类别查询

使用DropDownList下拉框 Repeater

        
            
                    //绑定显示的值
                        
标题 作者 时间 内容 类别 操作
<%# Eval("Title") %> <%# Eval("Author") %> <%# Eval("PushDate") %> <%# Eval("Conent") %> <%# Eval("Type") %> ' CommandName="More">查看详情

在此编写代码

 public partial class Defult : System.Web.UI.Page
    {
        
    }
public partial class Defult : System.Web.UI.Page
    {
        //实例化
        ArticlesService articlesService = new ArticlesService();
        CategoryService categoryService = new CategoryService();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //回传
            if (!IsPostBack)
            {
                this.Repeater1.DataSource = articlesService.GetArticles();
                this.Repeater1.DataBind();
                
                //绑定类别值
                this.DropDownList1.DataSource = categoryService.GetCategory();
                this.DropDownList1.DataBind();
            }
        }

       
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int type =int.Parse( DropDownList1.SelectedValue);

            this.Repeater1.DataSource = articlesService.SelectByType(type);
            
            this.Repeater1.DataBind();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //查看详情 传值
            int id = int.Parse(e.CommandArgument.ToString());
            //判断
            if (e.CommandName=="More")
            {
                Response.Redirect("/Datail.aspx?id="+id);
            }
        }
    }

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/904028.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号