访问类
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 }