zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    NGINX反向代理与负载均衡
    kubernetes介绍
    Linux下yum出现no module named pycurl 解决办法
    MySQL中间件介绍
    Memcached做Tomcat的session共享
    MySQL高负载优化
    Centos下安装Tomcat7
    浅谈世界坐标系,相机坐标系,图像坐标系,像素坐标系的关系
    相机标定方法之初探
    ubuntu18.04安装kalibr相机标定工具
  • 原文地址:https://www.cnblogs.com/yangwang365/p/8244707.html
Copyright © 2011-2022 走看看