表和实体类之间的转换

using System;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using Mamarow.SysUtils;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Mamarow.DBUtil
{
     public static class Common 
    {
         public static DateTime GetServerDateTime()
         {
             string strSQL = "select sysdate  from dual";
             DbSess dbSess = new DbSess();
             return  Convert.ToDateTime(dbSess.DoneScalar(strSQL, null));
         }

         /// <summary>
         /// 返回值
         /// Datatable转化为实体类 忽略大小写
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="dt"></param>
         /// <returns></returns>
         public static List<T> CCTableToEntNo<T>(DataTable dt,bool t) where T : new()
         {
             List<T> lts = new List<T>();
             PropertyInfo[] pIFs = typeof(T).GetProperties();
             if (dt == null || dt.Rows.Count <= 0) return lts;
             for (int j = 0; j < dt.Rows.Count; j++)
             {
                 T tT = new T();
                 DataColumnCollection dcc = dt.Columns;
                 for (int i = 0; i < dt.Columns.Count; i++)
                 {
                     foreach (PropertyInfo propertyInfo in pIFs)
                     {
                         if (string.Compare(dt.Columns[i].ColumnName, propertyInfo.Name, true) != 0) continue;
                         PropertyInfo pr = tT.GetType().GetProperty(propertyInfo.Name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                         string Ptypename = propertyInfo.PropertyType.Name.ToLower();
                         string Rtypename = dt.Rows[j][i].GetType().ToString().ToLower();
                         if (dt.Rows[j][i] != DBNull.Value)
                         {
                             if (Ptypename.Contains("int"))
                                 pr.SetValue(tT, Convert.ToInt32(dt.Rows[j][i]), null);
                             else if (Ptypename.Contains("string"))
                                 pr.SetValue(tT, Convert.ToString(dt.Rows[j][i]), null);
                             else
                                 pr.SetValue(tT, dt.Rows[j][i], null);
                             break;
                         }
                     }
                 }
                 lts.Add(tT);
             }
             return lts;
         }
         /// <summary>
         /// 返回值
         /// Datatable转化为实体类 忽略大小写
         /// !智能检测所有的列都有赋值成功!
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="dt"></param>
         /// <returns></returns>
         public static List<T> CCTableToEntNo<T>(DataTable dt) where T : new()
         {
             try
             {
                 List<T> lts = new List<T>();
                 PropertyInfo[] pIFs = typeof(T).GetProperties();
                 if (dt == null || dt.Rows.Count <= 0) return lts;
                 for (int j = 0; j < dt.Rows.Count; j++)
                 {
                     T tT = new T();
                     bool iChecked = false;
                     DataColumnCollection dcc = dt.Columns;
                     for (int i = 0; i < dt.Columns.Count; i++)
                     {
                         if (i > 0 && !iChecked) throw new Exception("DataTable 的 " + dt.Columns[i].ColumnName + " 没有找到实体类对应的属性");
                         iChecked = false;
                         foreach (PropertyInfo propertyInfo in pIFs)
                         {
                             if (string.Compare(dt.Columns[i].ColumnName, propertyInfo.Name, true) != 0) continue;
                             PropertyInfo pr = tT.GetType().GetProperty(propertyInfo.Name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                             string Ptypename = propertyInfo.PropertyType.Name.ToLower();
                             string Rtypename = dt.Rows[j][i].GetType().ToString().ToLower();
                             if (dt.Rows[j][i] != DBNull.Value)
                             {
                                 if (Ptypename.Contains("int"))
                                     pr.SetValue(tT, Convert.ToInt32(dt.Rows[j][i]), null);
                                 else if (Ptypename.Contains("string"))
                                     pr.SetValue(tT, Convert.ToString(dt.Rows[j][i]), null);
                                 else
                                     pr.SetValue(tT, dt.Rows[j][i], null);
                                 iChecked = true;
                                 break;
                             }
                         }
                     }
                     lts.Add(tT);
                 }
                 return lts;
             }
             catch (Exception ex)
             {
                 throw;
             }
         }

         /// <summary>
         /// 返回值
         /// Datatable转化为实体类
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="dt"></param>
         /// <returns></returns>
         public static List<T> CCTableToEnt<T>(DataTable dt) where T : new()
         {
             try
             {
                 bool iChecked = false;
                 List<T> lts = new List<T>();
                 PropertyInfo[] pIFs = typeof(T).GetProperties();
                 if (dt == null || dt.Rows.Count <= 0) return lts;
                 for (int j = 0; j < dt.Rows.Count; j++)
                 {
                     if (j > 0 && !iChecked) throw new Exception("DataTable 的 " + dt.Columns[j].ColumnName + " 没有找到实体类对应的属性");
                     iChecked = false;
                     T tT = new T();
                     DataColumnCollection dcc = dt.Columns;
                     foreach (PropertyInfo propertyInfo in pIFs)
                     {
                         if (!dt.Columns.Contains(propertyInfo.Name)) continue;
                         PropertyInfo pr = tT.GetType().GetProperty(propertyInfo.Name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                         string Ptypename = propertyInfo.PropertyType.Name.ToLower();
                         string Rtypename = dt.Rows[j][propertyInfo.Name].GetType().ToString().ToLower();
                         if (dt.Rows[j][propertyInfo.Name] != DBNull.Value)
                         {
                             if (Ptypename.Contains("int"))
                                 pr.SetValue(tT, Convert.ToInt32(dt.Rows[j][propertyInfo.Name]), null);
                             else if (Ptypename.Contains("string"))
                                 pr.SetValue(tT, Convert.ToString(dt.Rows[j][propertyInfo.Name]), null);
                             else
                                 pr.SetValue(tT, dt.Rows[j][propertyInfo.Name], null);
                             iChecked = true;
                             break;
                         }
                     }
                     lts.Add(tT);
                 }
                 return lts;
             }
             catch (Exception ex)
             {
                 throw;
             }

             

         }
    }
}

 

转载于:https://www.cnblogs.com/xihuafeiyu/articles/3734458.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值