- 建立数据库(BOOK)
- 建立数据表(Articles,Category)
表 Articles
| 字段名 | 类型 | 约束 |
|---|---|---|
| ID | int | 主键 自增 非空 |
| Title | nvarchar(50) | 非空 |
| Author | nvarchar(50) | 非空 |
| PushDate | datetime | 获取当前时间 |
| Conent | nvarchar(200) | 非空 |
| Type | int | 外键约束ID |
表 Category
| 字段名 | 类型 | 约束 |
|---|---|---|
| ID | int | 主键 自增 非空 |
| Name | nvarchar(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);
}
}
}



