本文共 3767 字,大约阅读时间需要 12 分钟。
1 public static class DataUtil 2 { 3 #region dataConvert 4 ///5 /// 从数据行返回数据实体对象 6 /// 7 ///数据实体类型 8 /// 数据行 9 ///数据实体对象 10 public static T GetModel(DataRow row) where T : class, new() 11 { 12 if (row == null) return null; 13 14 T result = new T(); 15 16 Type type = typeof(T); 17 18 System.Reflection.PropertyInfo[] ps = type.GetProperties(); 19 20 foreach (System.Reflection.PropertyInfo p in ps) 21 { 22 if (row.Table.Columns.Contains(p.Name)) 23 { 24 object value = row[p.Name]; 25 26 if (!(value is DBNull)) 27 { 28 try 29 { 30 p.SetValue(result, value, null); 31 } 32 catch 33 { 34 35 } 36 } 37 } 38 } 39 40 return result; 41 } 42 /// 43 /// 从数据表返回数据实体列表对象 44 /// 45 ///数据实体类型 46 /// 数据表 47 ///数据实体列表对象 48 public static ListGetModelList (DataTable dt) where T : class, new() 49 { 50 List list = new List (); 51 foreach (DataRow row in dt.Rows) 52 { 53 list.Add(GetModel (row)); 54 } 55 return list; 56 } 57 58 /// 59 /// 从数据行集合返回数据实体列表对象 60 /// 61 ///数据实体类型 62 /// 数据行集合 63 ///数据实体列表对象 64 public static ListGetModelList (IEnumerable rows) where T : class, new() 65 { 66 List list = new List (); 67 foreach (DataRow row in rows) 68 { 69 list.Add(GetModel (row)); 70 } 71 return list; 72 } 73 #endregion 74 } 75 public static class CommonUtil 76 { 77 public static string GetData(string url) 78 { 79 string sReturn; 80 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 81 req.ContentType = "multipart/form-data"; 82 WebResponse resp = req.GetResponse(); 83 StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8); 84 sReturn = sr.ReadToEnd().Trim(); 85 resp.Close(); 86 sr.Close(); 87 return sReturn; 88 } 89 /// 90 /// 时间戳转为C#格式时间 91 /// 92 /// 93 ///94 public static DateTime GetTime(string timeStamp) 95 { 96 DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 97 long lTime = long.Parse(timeStamp + "0000000"); 98 TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); 99 }100 101 /// 102 /// DateTime时间格式转换为Unix时间戳格式103 /// 104 /// 105 ///106 public static int ConvertDateTimeInt(System.DateTime time)107 {108 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));109 return (int)(time - startTime).TotalSeconds;110 }111 public static string SubStr(string content, int length)112 {113 return NoHTML(content).Length > length ? NoHTML(content).Trim().Substring(0, length) + "..." : NoHTML(content);114 }115 public static string NoHTML(string Htmlstring)116 {117 //删除脚本 118 Htmlstring = Regex.Replace(Htmlstring, @"
转载地址:http://ukzlo.baihongyu.com/