/*根据newsId获取某条新闻信息记录*/ public static ENTITY.News getSomeNews(int newsId) { /*构建查询sql*/ string sql = "select * from News where newsId=" + newsId; SqlDataReader DataRead = DBHelp.ExecuteReader(sql, null); ENTITY.News news = new ENTITY.News(); /*如果查询存在记录,就包装到对象中返回*/ if (DataRead.Read()) { news.newsId = Convert.ToInt32(DataRead["newsId"]); news.newsTitle = DataRead["newsTitle"].ToString(); news.newsContent = DataRead["newsContent"].ToString(); news.newsDate = Convert.ToDateTime(DataRead["newsDate"].ToString()); news.newsPhoto = DataRead["newsPhoto"].ToString(); } return(news); }
/*添加新闻信息实现*/ public static bool AddNews(ENTITY.News news) { string sql = "insert into News(newsTitle,newsContent,newsDate,newsPhoto) values(@newsTitle,@newsContent,@newsDate,@newsPhoto)"; /*构建sql参数*/ SqlParameter[] parm = new SqlParameter[] { new SqlParameter("@newsTitle", SqlDbType.VarChar), new SqlParameter("@newsContent", SqlDbType.VarChar), new SqlParameter("@newsDate", SqlDbType.DateTime), new SqlParameter("@newsPhoto", SqlDbType.VarChar) }; /*给参数赋值*/ parm[0].Value = news.newsTitle; //新闻标题 parm[1].Value = news.newsContent; //新闻内容 parm[2].Value = news.newsDate; //发布日期 parm[3].Value = news.newsPhoto; //新闻图片 /*执行sql进行添加*/ return((DBHelp.ExecuteNonQuery(sql, parm) > 0) ? true : false); }
/*更新新闻信息实现*/ public static bool EditNews(ENTITY.News news) { string sql = "update News set newsTitle=@newsTitle,newsContent=@newsContent,newsDate=@newsDate,newsPhoto=@newsPhoto where newsId=@newsId"; /*构建sql参数信息*/ SqlParameter[] parm = new SqlParameter[] { new SqlParameter("@newsTitle", SqlDbType.VarChar), new SqlParameter("@newsContent", SqlDbType.VarChar), new SqlParameter("@newsDate", SqlDbType.DateTime), new SqlParameter("@newsPhoto", SqlDbType.VarChar), new SqlParameter("@newsId", SqlDbType.Int) }; /*为参数赋值*/ parm[0].Value = news.newsTitle; parm[1].Value = news.newsContent; parm[2].Value = news.newsDate; parm[3].Value = news.newsPhoto; parm[4].Value = news.newsId; /*执行更新*/ return((DBHelp.ExecuteNonQuery(sql, parm) > 0) ? true : false); }