首页 > 其他 > 详细

C# 实体类与数据表之间的映射

时间:2014-04-10 22:49:08      阅读:1051      评论:0      收藏:0      [点我收藏+]

 public class BaseEntity<T> where T : BaseEntity<T>
    {
        public BaseEntity()
        {

        }

        public BaseEntity(DataTable table, int indexRow)
        {
            InitObjInfo(table, indexRow);
        }

        /// <summary>
        /// 初始化实体类对象信息
        /// </summary>
        /// <param name="table"></param>
        /// <param name="indexRow"></param>
        private void InitObjInfo(DataTable table, int indexRow)
        {
            for (int i = 0; i < table.Columns.Count; i++)
            {
                // 按列的名称,从当前对象中获取同名属性
                PropertyInfo pInfo = this.GetType().GetProperty(table.Columns[i].ColumnName);

                // 存在该属性
                if (pInfo != null)
                {
                    object value = table.Rows[indexRow][table.Columns[i].ColumnName];
                    value = value == DBNull.Value ? string.Empty : value;
                    // 如果对象属性定义的类型和table的列的类型一致
                    if (pInfo.PropertyType == table.Columns[i].DataType)
                    {
                        pInfo.SetValue(this, value, null);
                    }
                    else
                    {
                        // 如果对象类型是枚举类型
                        if (pInfo.PropertyType.IsEnum)
                        {
                            // 数据库中保存的是int类型,则直接为枚举赋值
                            if (value.GetType() == typeof(int))
                            {
                                pInfo.SetValue(this, value, null);
                            }

                            // 如果数据库中保存的是string类型
                            if (value.GetType() == typeof(string))
                            {
                                pInfo.SetValue(this, Enum.Parse(pInfo.PropertyType, value.ToString(), false), null);//赋值
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 返回一个类型为T的对象
        /// </summary>
        /// <param name="table">数据表</param>
        /// <param name="rowIndex">表中的第几行</param>
        /// <returns>object</returns>
        protected static T CreateInstance(DataTable table, int rowIndex)
        {
            if (table == null)
            {
                return null;
            }
            if (table.Rows.Count > rowIndex)
            {
                // 使用反射创建对象
                return (T)System.Activator.CreateInstance(typeof(T), table, rowIndex);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 返回一个泛型列表
        /// </summary>
        /// <param name="table">数据表</param>
        /// <returns></returns>
        protected static List<T> CreateInstances(DataTable table)
        {
            List<T> instances = new List<T>();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                T instance = CreateInstance(table, i);
                if (instance != null)
                {
                    instances.Add(instance);
                }
            }
            return instances;
        }
    }

C# 实体类与数据表之间的映射,布布扣,bubuko.com

C# 实体类与数据表之间的映射

原文:http://www.cnblogs.com/thinker-cxz/p/3656822.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!