DataSet转换成List<>

方法一:

 //DataSet转换成List<ArticleInfo>
        public List<ArticleInfo> GetArticleList(DataSet ds)
         {
             List<ArticleInfo> list = new List<ArticleInfo>();
             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
             {
                 ArticleInfo model = new ArticleInfo();
                 model.arttime = Convert.ToDateTime(ds.Tables[0].Rows[i]["arttime"]);
                 model.cgyid = Convert.ToInt32(ds.Tables[0].Rows[i]["cgyid"]);
                 model.clicks = Convert.ToInt32(ds.Tables[0].Rows[i]["clicks"]);
                 model.contents = Convert.ToString(ds.Tables[0].Rows[i]["contents"]);
                 model.id = Convert.ToInt32(ds.Tables[0].Rows[i]["id"]);
                 model.img = Convert.ToString(ds.Tables[0].Rows[i]["img"]);
                model.recommend = Convert.ToBoolean(ds.Tables[0].Rows[i]["recommend"]);
                 model.related = Convert.ToBoolean(ds.Tables[0].Rows[i]["related"]);
                 model.title = Convert.ToString(ds.Tables[0].Rows[i]["title"]);
                model.uid = Convert.ToInt32(ds.Tables[0].Rows[i]["uid"]);
                 list.Add(model);
             }
             return list;
         }

方法二:

  public List<ArticleInfo> DataSetToList(DataSet ds)
         {
             List<ArticleInfo> list = new List<ArticleInfo>();
             foreach (DataRow row in ds.Tables[0].Rows)
             {
                 list.Add(DataRowToModel(row));
             }
             return list;
         }


 

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ArticleInfo DataRowToModel(DataRow row)
        {
            ArticleInfo model = new ArticleInfo();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }

                if (row["contents"] != null)
                {
                    model.contents = row["contents"].ToString();
                }

                if (row["cgyid"] != null && row["cgyid"].ToString() != "")
                {
                    model.cgyid = int.Parse(row["cgyid"].ToString());
                }

                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }

                if (row["arttime"] != null && row["arttime"].ToString() != "")
                {
                    model.arttime = DateTime.Parse(row["arttime"].ToString());
                }

                if (row["uid"] != null && row["uid"].ToString() != "")
                {
                    model.uid = int.Parse(row["uid"].ToString());
                }

                if (row["recommend"] != null && row["recommend"].ToString() != "")
                {
                    if ((row["recommend"].ToString() == "1") || (row["recommend"].ToString().ToLower() == "true"))
                    {
                        model.recommend = true;
                    }
                    else
                    {
                        model.recommend = false;
                    }
                }
                if (row["img"] != null)
                {
                    model.img = row["img"].ToString();
                }

                if (row["clicks"] != null && row["clicks"].ToString() != "")
                {
                    model.clicks = int.Parse(row["clicks"].ToString());
                }

                if (row["related"] != null && row["related"].ToString() != "")
                {
                    if ((row["related"].ToString() == "1") || (row["related"].ToString().ToLower() == "true"))
                    {
                        model.related = true;
                    }
                    else
                    {
                        model.related = false;
                    }
                }

            }
            return model;
        }

 

posted @ 2018-01-08 20:18  我就是仰望  阅读(947)  评论(0编辑  收藏  举报