Newer
Older
RbFreqStand / RbFreqStandMeasure / tools / DataHelper.cs
yangqianqian on 7 Apr 2021 4 KB homepage complete
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace Casic.Birmm.RbFreqStandMeasure.tools
{
    static class DataHelper
    {
       
        public static T DataRowToModel<T>(DataRow row)
        {
            T t = default(T);
            PropertyInfo[] propertypes = null;
            string tempName = string.Empty;
            t = Activator.CreateInstance<T>();
            propertypes = t.GetType().GetProperties();
            foreach (PropertyInfo pro in propertypes)
            {
                tempName = pro.Name;
                if (row.Table.Columns.Contains(tempName))
                {
                    object value = row[tempName];
                    if (value.GetType() == typeof(System.DBNull))
                    {
                        value = null;
                    }
                    pro.SetValue(t, value, null);
                }
            }
            return t;
        }

        /// <summary>
        /// DataTable转换为实体对象   
        /// </summary>
        public static T ConvertToEntity<T>(DataTable dt, DataRow row) where T : new()
        {
            T t = new T();
            try
            {
                System.Data.DataColumnCollection columns = dt.Columns;
                int iColumnCount = columns.Count;
                int i;
                int j;

                Type elementType;
                elementType = t.GetType();
                System.Reflection.PropertyInfo[] publicProperties = elementType.GetProperties();
                //if (!(publicProperties.Length > iColumnCount))
                //{
                for (i = 0; i < iColumnCount; i++)
                {
                    for (j = 0; j < publicProperties.Length; j++)
                    {
                        if (columns[i].ColumnName.ToLower().Replace("_", "") == publicProperties[j].Name.ToLower().Replace("_", ""))
                        {

                            if (publicProperties[j].PropertyType == typeof(int))
                            {
                                int num = 0;
                                try
                                {
                                    num = Convert.ToInt32(row[i]);
                                }
                                catch
                                {
                                }
                                publicProperties[j].SetValue(t, num, null);
                            }
                            else if (publicProperties[j].PropertyType == typeof(string) && row[i] == System.DBNull.Value)
                            {
                                publicProperties[j].SetValue(t, null, null);
                            }
                            else if (columns[i].DataType == typeof(byte[]))
                            {
                                object value = row[i] == System.DBNull.Value ? null : Convert.ToBase64String((byte[])row[i]);
                                publicProperties[j].SetValue(t, value, null);
                            }
                            else
                            {
                                object value = row[i] == System.DBNull.Value ? null : row[i];
                                publicProperties[j].SetValue(t, value, null);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return t;
        }


        public static DataTable ConvertToDataTable<T>(this IEnumerable<T> list)
        {
            DataTable dtResult = new DataTable();
            List<PropertyInfo> propertiyInfos = new List<PropertyInfo>();
            //生成各列
            Array.ForEach<PropertyInfo>(typeof(T).GetProperties(), p =>
            {
                propertiyInfos.Add(p);
                dtResult.Columns.Add(p.Name, p.PropertyType);
            });
            //生成各行
            foreach (var item in list)
            {
                if (item == null)
                {
                    continue;
                }
                DataRow dataRow = dtResult.NewRow();
                propertiyInfos.ForEach(p => dataRow[p.Name] = p.GetValue(item, null));
                dtResult.Rows.Add(dataRow);
            }
            return dtResult;
        }

    }
}