先建三層構(gòu)造(DataModel、DAL迷守、BLL)
1.在DAL層添加2個(gè)類
1.>? ? public class CatelogDAO
? ? {
? ? ? ? // 查詢所有的類別
? ? ? ? public? List<Catelog>? Select()
? ? ? ? {
? ? ? ? ? ? MyDBEntities db = new MyDBEntities();
? ? ? ? ? ? //? return db.Catelog.Select(p=>p).ToList();
? ? ? ? ? ? var result = from catelog in db.Catelog
? ? ? ? ? ? ? ? ? ? ? ? select catelog;
? ? ? ? ? ? return result.ToList();
? ? ? ? }
2.>? ?? public class ArticleDAO
? ? {
? ? ? // 查詢所有的文章信息
? ? ? public? List<Article> Select()
? ? ? ? {
? ? ? ? ? // New一個(gè)實(shí)體模型:
? ? ? ? ? ? MyDBEntities db = new MyDBEntities();
? ? ? ? ? return? db.Article.Select(a=>a).ToList();
? ? ? ? }
? ? ? ? // 根據(jù)id查找文章
? ? ? ? public Article Select(int? id)
? ? ? ? {
? ? ? ? ? ? MyDBEntities db = new MyDBEntities();
? ? ? ? ? ? return db.Article.Where(p=>p.Id==id).FirstOrDefault();
? ? ? ? }
? ? ? ? // 添加文章
? ? ? ? public int Add(Article article)
? ? ? ? {
? ? ? ? ? ? MyDBEntities db = new MyDBEntities();
? ? ? ? ? ? db.Article.Add(article);
? ? ? ? ? ? db.Entry<Catelog>(article.Catelog).State = System.Data.Entity.EntityState.Unchanged;
? ? ? ? ? return db.SaveChanges();
? ? ? ? }
? ? }
2.再在BLL層添加2個(gè)類
1.>? ? public class ArticleService
? ? {
? ? ? ? private ArticleDAO adao = new ArticleDAO();
? ? ? ? public? List<Article> Select()
? ? ? ? {
? ? ? ? ? ? return adao.Select();
? ? ? ? }
? ? ? ? public Article Select(int id)
? ? ? ? {
? ? ? ? ? ? return adao.Select(id);
? ? ? ? }
? ? ? ? public? int AddArticle(Article article)
? ? ? ? {
? ? ? ? ? ? if (String.IsNullOrEmpty(article.Title)){
? ? ? ? ? ? ? ? throw new Exception("標(biāo)題不能為空!");
? ? ? ? ? ? }
? ? ? ? ? ? return adao.Add(article);
? ? ? ? }
? ? }
2.>? ? public class CatelogServcie
? ? {
? ? ? ? private CatelogDAO cdao = new CatelogDAO();
? ? ? ? // 所有的類別
? ? ? ? public? List<Catelog>? Select()
? ? ? ? {
? ? ? ? ? ? return cdao.Select();
? ? ? ? }
? ? }
3.在項(xiàng)目中添加一個(gè)Web窗體
根據(jù)需求給出相應(yīng)的一些樣式
New2個(gè)方法:?CatelogServcie catelogService = new CatelogServcie();
? ? ? ? ArticleService articleService = new ArticleService();
在加載事件中顯示對(duì)應(yīng)數(shù)據(jù):
protected void Page_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? string? message=Request.QueryString["message"];
? ? ? ? ? ? if(!string.IsNullOrEmpty(message))
? ? ? ? ? ? this.Label1.Text = message;
? ? ? ? ? ? if (!IsPostBack)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.DdlCatelog.DataSource = catelogService.Select();
? ? ? ? ? ? ? ? this.DdlCatelog.DataBind();
? ? ? ? ? ? }
? ? ? ? }
給一個(gè)Button控件杭隙,在雙擊這個(gè)控件:
protected void Button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Article article = new Article();
? ? ? ? ? ? article.Title = this.TxtTitle.Text;
? ? ? ? ? ? article.Author = this.TxtAuthor.Text;
? ? ? ? ? ? article.Content = this.TxtContent.Text;
? ? ? ? ? ? article.PushTime = DateTime.Now;
? ? ? ? ? ? article.Catelog = new Catelog() { Id= int.Parse(this.DdlCatelog.SelectedValue) };
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int count = articleService.AddArticle(article);
? ? ? ? ? ? ? ? if (count > 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Response.Redirect("~/Add.aspx?message=添加成功!");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Response.Redirect("~/Add.aspx?message="+ex.Message);
? ? ? ? ? ? }
? ? ? ? }