예제 #1
0
        private static List <News> GetNewsBySql(string sql)
        {
            List <News> lists = new List <News>();

            DataTable dt = DBHelper.GetTable(sql);

            foreach (DataRow row in dt.Rows)
            {
                News news = new News();

                news.NewsId        = (int)row["NewsId"];
                news.Title         = row["Title"].ToString();
                news.PictureUrl    = row["PictureUrl"].ToString();
                news.PublishDate   = (DateTime)row["PublishDate"];
                news.PublisherName = row["PublisherName"].ToString();
                news.Clicks        = (int)row["Clicks"];
                news.State         = (int)row["State"];
                news.Contents      = row["Contents"].ToString();
                news.IsTop         = (int)row["IsTop"];
                //news.TypeId = (int)row["TypeId"];
                news.Type = NewsTypeService.GetNewsTypeByTypeId((int)row["typeId"]);
                lists.Add(news);
            }
            return(lists);
        }
예제 #2
0
        private static List <News> GetNewsBySql(string sql, params SqlParameter[] values)
        {
            List <News> list = new List <News>();

            try
            {
                DataTable table = DBHelper.GetDataTable(sql, values);

                foreach (DataRow row in table.Rows)
                {
                    News news = new News();

                    news.NewsId        = (int)row["NewsId"];
                    news.Title         = (string)row["Title"];
                    news.PictureUrl    = (string)row["PictureUrl"];
                    news.PublishDate   = (DateTime)row["PublishDate"];
                    news.PublisherName = (string)row["PublisherName"];
                    news.Clicks        = (int)row["Clicks"];
                    news.State         = (int)row["State"];
                    news.Contents      = (string)row["Contents"];
                    news.IsTop         = (int)row["IsTop"];
                    news.Type          = NewsTypeService.GetNewsTypeByTypeId((int)row["TypeId"]);            //FK

                    list.Add(news);
                }

                return(list);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
예제 #3
0
        public static News GetNewByNewsId(int newsId)
        {
            string sql = "SELECT * FROM News WHERE NewsId = @NewsId";

            int typeId;

            try
            {
                SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@NewsId", newsId));
                if (reader.Read())
                {
                    News news = new News();

                    news.NewsId        = (int)reader["NewsId"];
                    news.Title         = (string)reader["Title"];
                    news.PictureUrl    = (string)reader["PictureUrl"];
                    news.PublishDate   = (DateTime)reader["PublishDate"];
                    news.PublisherName = (string)reader["PublisherName"];
                    news.Clicks        = (int)reader["Clicks"];
                    news.State         = (int)reader["State"];
                    news.Contents      = (string)reader["Contents"];
                    news.IsTop         = (int)reader["IsTop"];
                    typeId             = (int)reader["TypeId"];         //FK

                    reader.Close();

                    news.Type = NewsTypeService.GetNewsTypeByTypeId(typeId);

                    return(news);
                }
                else
                {
                    reader.Close();
                    return(null);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
예제 #4
0
        //根据SQL语句查找所有的新闻
        public static List <News> GetAllNewsBySql(string strsql)
        {
            List <News> allnew = new List <News>();

            DataTable dt = DBHelper.GetTable(strsql);

            foreach (DataRow row in dt.Rows)
            {
                News news = new News();
                news.Newsid = int.Parse(row["NewsId"].ToString());
                //news.TypeId = int.Parse(row["TypeId"].ToString());
                news.Type          = NewsTypeService.GetAllTypeByTypeId((int)row["typeid"]);
                news.Pictureurl    = row["PictureUrl"].ToString();
                news.Publishdate   = Convert.ToDateTime(row["PublishDate"].ToString());
                news.Istop         = int.Parse(row["IsTop"].ToString());
                news.Publishername = row["PublisherName"].ToString();
                news.State         = int.Parse(row["State"].ToString());
                news.Titel         = row["Title"].ToString();
                news.Contents      = row["Contents"].ToString();

                allnew.Add(news);
            }
            return(allnew);
        }
예제 #5
0
        private static List <News> GetNewsBySql(string strsql)
        {
            DataTable   dt      = DBHelper.GetTable(strsql);
            List <News> allnews = new List <News>();

            foreach (DataRow row in dt.Rows)
            {
                News n = new News();
                n.NewsId = Convert.ToInt32(row["newsid"]);
                //n.TypeId = Convert.ToInt32(row["typeid"]);
                n.Type          = NewsTypeService.GetNewsTypeByTypeId((int)row["typeid"]);
                n.Title         = row["title"].ToString();
                n.PictureUrl    = row["pictureurl"].ToString();
                n.PublishDate   = Convert.ToDateTime(row["publishdate"]);
                n.PublisherName = row["publishername"].ToString();
                n.Clicks        = Convert.ToInt32(row["clicks"]);
                n.State         = Convert.ToInt32(row["state"]);
                n.Contents      = row["contents"].ToString();
                n.IsTop         = Convert.ToInt32(row["istop"]);

                allnews.Add(n);
            }
            return(allnews);
        }