Пример #1
0
        /// <summary>
        /// dataset转化为list的platinfo
        /// </summary>
        /// <param name="ds">datable</param>
        /// <returns></returns>
        public static List <PlatInfo> DS2List(DataTable dt)
        {
            List <PlatInfo> result = new List <PlatInfo>();

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                PlatInfo       temp      = (PlatInfo)Activator.CreateInstance(typeof(PlatInfo));
                PropertyInfo[] propertys = temp.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (pi.Name.Equals(dt.Columns[i].ColumnName))
                        {
                            // 数据库NULL值单独处理
                            if (dt.Rows[j][i] != DBNull.Value)
                            {
                                pi.SetValue(temp, dt.Rows[j][i], null);
                            }
                            else
                            {
                                pi.SetValue(temp, null, null);
                            }
                            break;
                        }
                    }
                }
                result.Add(temp);
            }
            return(result);
        }
Пример #2
0
 /// <summary>
 /// 将datatable转为platinfo对象
 /// </summary>
 /// <param name="dt"></param>
 /// <returns>不是一条结果就返回null</returns>
 public static PlatInfo Dt2pl(DataTable dt)
 {
     if (dt.Rows.Count != 1)
     {
         return(null);
     }
     else
     {
         PlatInfo       temp      = (PlatInfo)Activator.CreateInstance(typeof(PlatInfo));
         PropertyInfo[] propertys = temp.GetType().GetProperties();
         foreach (PropertyInfo pi in propertys)
         {
             for (int i = 0; i < dt.Columns.Count; i++)
             {
                 if (pi.Name.Equals(dt.Columns[i].ColumnName))
                 {
                     if (dt.Rows[0][i] != DBNull.Value)
                     {
                         pi.SetValue(temp, dt.Rows[0][i], null);
                     }
                     else
                     {
                         pi.SetValue(temp, null, null);
                     }
                     break;
                 }
             }
         }
         return(temp);
     }
 }