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; } } } }