访问类

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Data.Sql;
  7 using System.Data.SqlClient;
  8 using Models;
  9 
 10 namespace DAL
 11 {
 12    public class NewsService
 13     {
 14         //创建一个通用类
 15         private EFDBHelper helper = new EFDBHelper(new HotelDBEntities());
 16 
 17         /// <summary>
 18         /// 发布新闻:添加
 19         /// </summary>
 20         /// <param name="news"></param>
 21         /// <returns></returns>
 22         //public int PublishNews(News news)
 23         //{
 24         //    //using (HotelDBEntities db = new HotelDBEntities())
 25         //    //{
 26         //    //    db.News.Add(news);
 27         //    //   return  db.SaveChanges();
 28         //    //}
 29         //    return helper.Add<News>(news);
 30         //}
 31 
 32             //[下面是使用存储过程]
 33         public int PublishNews(News news)
 34         {
 35 
 36             SqlParameter[] param = new SqlParameter[]
 37                 {
 38                     new SqlParameter("@NewsTitle",news.NewsTitle),
 39                     new SqlParameter("@NewsContents",news.NewsContents),
 40                     new SqlParameter("@CategoryId",news.CategoryId),
 41                 };
 42             using (HotelDBEntities db = new DAL.HotelDBEntities())
 43             {
 44                 return db.Database.ExecuteSqlCommand("execute usp_AddNews @NewsTitle,@NewsContents,@CategoryId",param);
 45             }
 46         }
 47 
 48 
 49         //修改新闻
 50         public int ModifyNews(News news)
 51         {
 52             return helper.Modify<News>(news);
 53         }
 54 
 55 
 56         //删除新闻
 57         public int DeleteNews(int newsId)
 58         {
 59             News news = new News { NewsId = newsId };
 60             return helper.Delete<News>(news);
 61         }
 62 
 63             /// <summary>
 64             /// 查询指定前几条新闻
 65             /// </summary>
 66             /// <param name="count"></param>
 67             /// <returns></returns>
 68         public List<News> GetNews(int count)
 69         {
 70             List<News> list;
 71             using (HotelDBEntities db=new DAL.HotelDBEntities())
 72             {
 73                 return (from n in db.News
 74                         orderby n.PublishTime descending
 75                         select n).Take(count).ToList();
 76              }
 77         }
 78 
 79 
 80         /// <summary>
 81         /// 根据新闻Id查询新闻详细
 82         /// </summary>
 83         /// <param name="newsId"></param>
 84         /// <returns></returns>
 85         public News GetNewsById(int newsId)
 86         {
 87             using (HotelDBEntities db = new DAL.HotelDBEntities())
 88             {
 89               return    (from n in db.News where n.NewsId == newsId select n).FirstOrDefault();
 90             }
 91         }
 92 
 93 
 94         /// <summary>
 95         /// 获取所有的新闻分类
 96         /// </summary>
 97         /// <returns></returns>
 98         public List<NewsCategory> GetAllCategory()
 99         {
100             using (HotelDBEntities db = new DAL.HotelDBEntities())
101             {
102                 return (from c in db.NewsCategory select c).ToList(); 
103             }
104         }
105 
106         /// <summary>
107         /// 根据新闻分类ID查询分类名称
108         /// </summary>
109         /// <param name="categoryId"></param>
110         /// <returns></returns>
111         public string GetCategoryName(int categoryId)
112         {
113             using (HotelDBEntities db = new DAL.HotelDBEntities())
114             {
115                 return (from c in db.NewsCategory where c.CategoryId == categoryId select c.CategoryName).FirstOrDefault();
116             }
117         }
118     }
119 }

 

转载于:https://www.cnblogs.com/atlj/p/8647320.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值