新文章 网摘 文章 随笔 日记

使用SqlDataAdapter更新数据库如何获得自增Id, DataTable与强类型的相互转换,从Request上传的参数中获取值,并赋给强类型

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace Phoenix.Common.Infrastructure.Helpers
{
    public static class EntityTranslator
    {
        /// <summary>
        /// 将Datatable转换为实体List
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="dataTable">DataTable</param>
        /// <returns>List</returns>
        public static List<T> DataTableToEntityList<T>(this DataTable dataTable) where T : new()
        {
            List<T> lists = new List<T>();
            if (dataTable.Rows.Count > 0)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    lists.Add(DataRowToEntity(row, new T()));
                }
            }
            return lists;
        }
        /// <summary>
        /// 将DataRow转换成实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entity">实体实例</param>
        /// <param name="row">DATaTable的行</param>
        /// <returns></returns>
        public static T DataRowToEntity<T>(this DataRow row, T entity) where T : new()
        {
            //初始化 如果为null
            if (entity == null)
            {
                entity = new T();
            }
            //得到类型
            Type type = typeof(T);
            //取得属性集合
            PropertyInfo[] pi = type.GetProperties();
            foreach (PropertyInfo item in pi)
            {
                //给属性赋值
                if (row.Table.Columns.Contains(item.Name) && row[item.Name] != DBNull.Value)
                {
                    if (item.PropertyType == typeof(DateTime?))
                    {
                        item.SetValue(entity, Convert.ToDateTime(row[item.Name].ToString()), null);
                    }
                    else
                    {
                        item.SetValue(entity, ChangeType(row[item.Name], item.PropertyType), null);
                    }
                }
            }
            return entity;
        }
        /// <summary>
        /// 将DataRow转换成实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row">The row.</param>
        /// <returns></returns>
        public static T DataRowToEntity<T>(this DataRow row) where T : new()
        {
            T entity = Activator.CreateInstance<T>();
            DataRowToEntity(row, entity);
            return entity;
        }
        /// <summary>
        /// 将实体转换为DataRow
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entity">实体实例</param>
        /// <param name="row">DataTable的行</param>
        /// <returns></returns>
        public static DataRow EntityToDataRow<T>(this T entity, DataRow row) where T : new()
        {
            //初始化 如果为null
            if (entity == null)
            {
                entity = new T();
            }
            //得到类型
            Type type = typeof(T);
            //取得属性集合
            PropertyInfo[] pi = type.GetProperties();
            foreach (PropertyInfo item in pi)
            {
                //给属性赋值
                if (row[item.Name] != null)
                {
                    row[item.Name] = item.GetValue(entity);
                }
            }
            return row;
        }
        /// <summary>
        /// 将List转换为DataTable
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="list">实体列表</param>
        /// <param name="dataTable">DataTable</param>
        /// <returns></returns>
        public static DataTable EntityListToDataTable<T>(this List<T> list, DataTable dataTable) where T : new()
        {
            foreach (var item in list)
            {
                DataRow newRow = dataTable.NewRow();
                EntityToDataRow(item, newRow);
            }
            return dataTable;
        }

        /// <summary>
        /// 通用的数据类型转换方法
        /// </summary>
        /// <param name="value">来源的值</param>
        /// <param name="type">目标类型</param>
        /// <returns>目标类型值</returns>
        public static object ChangeType(object value, Type type)
        {
            if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
            if (value == null) return null;
            if (type == value.GetType()) return value;
            if (type.IsEnum)
            {
                if (value is string)
                    return Enum.Parse(type, value as string);
                else
                    return Enum.ToObject(type, value);
            }
            if (!type.IsInterface && type.IsGenericType)
            {
                Type innerType = type.GetGenericArguments()[0];
                object innerValue = ChangeType(value, innerType);
                return Activator.CreateInstance(type, new object[] { innerValue });
            }
            if (value is string && type == typeof(Guid)) return new Guid(value as string);
            if (value is string && type == typeof(Version)) return new Version(value as string);
            if (!(value is IConvertible)) return value;
            return Convert.ChangeType(value, type);
        }
    }
}

 

posted @ 2020-11-17 14:39  岭南春  阅读(127)  评论(0)    收藏  举报