• DataReader,DataTable利用泛型填充实体类


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    ///TestTableModel 的摘要说明
    /// </summary>
    public class TestTableModel
    {
        public int D_Id { get; set; }
        public string D_Name { get; set; }
        public string D_Password { get; set; }
        public string D_Else { get; set; }
        public decimal D_Amount { get; set; }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Reflection;
    
    namespace MSCL
    {
        /// <summary>
        ///ObjectToList 的摘要说明
        /// </summary>
        public static class ObjectToList
        {
            /*  --示例
                IDataReader dr = MSCL.SqlHelper.GetSqlDataReader("select * from TestTable where d_id in(6,7,8)");
                List<TestTableModel> t1 = MSCL.ObjectToList.DataReaderToList<TestTableModel>(dr);
                for (int i = 0; i < t1.Count; i++)
                {
                    Response.Write(t1[i].D_Id + "<br/>");
                    Response.Write(t1[i].D_Name + "<br/>");
                    Response.Write(t1[i].D_Password + "<br/>");
                    Response.Write(t1[i].D_Else + "<br/>");
                    Response.Write(t1[i].D_Amount + "<br/>");
                }   
                dr.Dispose();
                dr.Close();
             */
    
            /// <summary>
            /// DataReader利用泛型填充实体类
            /// </summary>
            /// <typeparam name="T">实体类</typeparam>
            /// <param name="reader">DataReader</param>
            /// <returns></returns>
            public static List<T> DataReaderToList<T>(IDataReader reader)
            {
                //实例化一个List<>泛型集合
                List<T> DataList = new List<T>();
                while (reader.Read())
                {
                    T RowInstance = Activator.CreateInstance<T>();//动态创建数据实体对象
                    //通过反射取得对象所有的Property
                    foreach (PropertyInfo Property in typeof(T).GetProperties())
                    {
                        try
                        {
                            //取得当前数据库字段的顺序
                            int Ordinal = reader.GetOrdinal(Property.Name);
                            if (reader.GetValue(Ordinal) != DBNull.Value)
                            {
                                //将DataReader读取出来的数据填充到对象实体的属性里
                                Property.SetValue(RowInstance, Convert.ChangeType(reader.GetValue(Ordinal), Property.PropertyType), null);
                            }
                        }
                        catch
                        {
                            break;
                        }
                    }
                    DataList.Add(RowInstance);
                }
                return DataList;
            }
    
            /// <summary>
            /// DataTable利用泛型填充实体类
            /// </summary>
            /// <typeparam name="T">实体类</typeparam>
            /// <param name="dt">DataTable</param>
            /// <returns></returns>
            public static List<T> DataTableToList<T>(DataTable dt) where T : new()
            {
                var list = new List<T>();
                if (dt == null) return list;
                var len = dt.Rows.Count;
    
                for (var i = 0; i < len; i++)
                {
                    var info = new T();
                    foreach (DataColumn dc in dt.Rows[i].Table.Columns)
                    {
                        var field = dc.ColumnName;
                        var value = dt.Rows[i][field].ToString();
                        if (string.IsNullOrEmpty(value)) continue;
                        if (IsDate(value))
                        {
                            value = DateTime.Parse(value).ToString();
                        }
    
                        var p = info.GetType().GetProperty(field);
    
                        try
                        {
                            if (p.PropertyType == typeof(string))
                            {
                                p.SetValue(info, value, null);
                            }
                            else if (p.PropertyType == typeof(int))
                            {
                                p.SetValue(info, int.Parse(value), null);
                            }
                            else if (p.PropertyType == typeof(bool))
                            {
                                p.SetValue(info, bool.Parse(value), null);
                            }
                            else if (p.PropertyType == typeof(DateTime))
                            {
                                p.SetValue(info, DateTime.Parse(value), null);
                            }
                            else if (p.PropertyType == typeof(float))
                            {
                                p.SetValue(info, float.Parse(value), null);
                            }
                            else if (p.PropertyType == typeof(double))
                            {
                                p.SetValue(info, double.Parse(value), null);
                            }
                            else
                            {
                                p.SetValue(info, value, null);
                            }
                        }
                        catch (Exception)
                        {
                            //p.SetValue(info, ex.Message, null); 
                        }
                    }
                    list.Add(info);
                }
                dt.Dispose(); dt = null;
                return list;
            }
    
            /// <summary>
            /// 是否是时间
            /// </summary>
            /// <param name="d"></param>
            /// <returns></returns>
            private static bool IsDate(string d)
            {
                DateTime d1;
                double d2;
                return !double.TryParse(d, out d2) && DateTime.TryParse(d, out d1);
            }
        }
    }

  • 相关阅读:
    Mybatis 使用 mapper 接口规范的 一对一, 一对多,多对多映射
    mybatis mapper接口开发dao层
    使用 maven 搭建web开发基本架构
    mybatis dao 层开发简易版 非整合 spring
    mybatis 不整合spring 入门小例子
    spring+spring mvc+JdbcTemplate 入门小例子
    PythonCharm 配置本地反向代理激活
    Python 算法实现
    不写一行代码,绿色三层我也行
    pythonday
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234256.html
Copyright © 2020-2023  润新知