DataUtil

简介: 1 public static class DataUtil 2 { 3 #region dataConvert 4 /// 5 /// 从数据行返回数据实体对象 6 /// 7...
  1 public static class DataUtil
  2     {
  3         #region dataConvert
  4         /// <summary>
  5         /// 从数据行返回数据实体对象
  6         /// </summary>
  7         /// <typeparam name="T">数据实体类型</typeparam>
  8         /// <param name="row">数据行</param>
  9         /// <returns>数据实体对象</returns>
 10         public static T GetModel<T>(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         /// <summary>
 43         /// 从数据表返回数据实体列表对象
 44         /// </summary>
 45         /// <typeparam name="T">数据实体类型</typeparam>
 46         /// <param name="dt">数据表</param>
 47         /// <returns>数据实体列表对象</returns>
 48         public static List<T> GetModelList<T>(DataTable dt) where T : class, new()
 49         {
 50             List<T> list = new List<T>();
 51             foreach (DataRow row in dt.Rows)
 52             {
 53                 list.Add(GetModel<T>(row));
 54             }
 55             return list;
 56         }
 57 
 58         /// <summary>
 59         /// 从数据行集合返回数据实体列表对象
 60         /// </summary>
 61         /// <typeparam name="T">数据实体类型</typeparam>
 62         /// <param name="rows">数据行集合</param>
 63         /// <returns>数据实体列表对象</returns>
 64         public static List<T> GetModelList<T>(IEnumerable<DataRow> rows) where T : class, new()
 65         {
 66             List<T> list = new List<T>();
 67             foreach (DataRow row in rows)
 68             {
 69                 list.Add(GetModel<T>(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         /// <summary>
 90         /// 时间戳转为C#格式时间
 91         /// </summary>
 92         /// <param name=”timeStamp”></param>
 93         /// <returns></returns>
 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         /// <summary>
102         /// DateTime时间格式转换为Unix时间戳格式
103         /// </summary>
104         /// <param name=”time”></param>
105         /// <returns></returns>
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, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
119             //删除HTML   
120             Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
121             Htmlstring = Regex.Replace(Htmlstring, @"([/r/n])[/s]+", "", RegexOptions.IgnoreCase);
122             Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
123             Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
124             Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "/", RegexOptions.IgnoreCase);
125             Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
126             Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
127             Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
128             Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
129             Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "/xa1", RegexOptions.IgnoreCase);
130             Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "/xa2", RegexOptions.IgnoreCase);
131             Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "/xa3", RegexOptions.IgnoreCase);
132             Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "/xa9", RegexOptions.IgnoreCase);
133             Htmlstring = Regex.Replace(Htmlstring, @"&#(/d+);", "", RegexOptions.IgnoreCase);
134             //替换掉 < 和 > 标记
135             Htmlstring.Replace("<", "");
136             Htmlstring.Replace(">", "");
137             Htmlstring.Replace("/r/n", "");
138             //返回去掉html标记的字符串
139             return Htmlstring;
140         }
141         public static string GetCharABC(int index)
142         {
143             if (index > 25) { 
144                 return  (index+1).ToString(); }
145             string[] abcStr = new string[] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
146             return abcStr[index];
147         }
148         public static string GetShortTime(DateTime time)
149         {
150             try
151             {
152                 if (time.ToShortDateString() == DateTime.Now.ToShortDateString())
153                 {
154                     return time.ToString("HH:mm");
155                 }
156                 else if (time.Year != DateTime.Now.Year) {
157                     return time.ToString("yyyy-MM-dd");
158                 }
159                 else
160                 {
161                     return time.ToString("MM-dd");
162                 }
163             }
164             catch (Exception)
165             {
166                 return DateTime.Now.ToString("MM-dd");
167             }
168 
169          
170         }
171         public static string ContentTypConvert(string ptype)
172         {
173             switch (ptype)
174             {
175                 case "application/msword":
176                     ptype = ".doc";
177                     break;
178                 case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
179                     ptype = ".docx";
180                     break;
181                 case "application/pdf":
182                     ptype = ".pdf";
183                     break;
184                 case "application/vnd.ms-excel":
185                     ptype = ".xls";
186                     break;
187                 case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
188                     ptype = ".xlsx";
189                     break;//application/octet-stream 和 application/x-zip-compressed
190                 case "application/octet-stream":
191                     ptype = ".rar";
192                     break;
193                 case "application/x-zip-compressed":
194                     ptype = ".zip";
195                     break;
196                 case "image/jpeg":
197                     ptype = ".jpg";
198                     break;
199                 case "image/png":
200                     ptype = ".png";
201                     break;
202                 default:
203                     ptype = "";
204                     break;
205             } 
206             return ptype;
207         }
208         /// <summary>
209         /// 特殊字符清空
210         /// </summary>
211         /// <param name="strMessage"></param>
212         /// <returns></returns>
213         public static string StringClear(string strMessage)
214         {
215             string[] aryReg = { "!", "@", "#", "$", "^", "*", "'", "(", ")", "<", ">", "%", "\"\"", ",", ">=", "=<", "-", "_", ";", "||", "[", "]", "&", "/", "-", "|", "?", ":", " ", };
216             for (int i = 0; i < aryReg.Length; i++)
217             {
218                 strMessage = strMessage.Replace(aryReg[i], string.Empty);
219             }
220             return strMessage;
221         }
222        
223         public static List<string> GetStrImgList(string html)
224         {
225             List<string> list = new List<string>();
226             Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
227             // 搜索匹配的字符串 
228             MatchCollection matches = regImg.Matches(html);
229             // 取得匹配项列表 
230             foreach (Match match in matches) {
231                 list.Add(match.Groups["imgUrl"].Value);
232             }
233             return list;
234         }
235     }
View Code

 

目录
相关文章
|
8月前
|
容器
sprintboot读取自定义配置文件properties、yml、yaml,环境springboot2.4.4
我这里使用的是springboot2.4.4的版本,其他版本自测
|
NoSQL Redis
redis启动报错-Could not connect to Redis at 127.0.0.1:6379: Connection refused
redis启动报错-Could not connect to Redis at 127.0.0.1:6379: Connection refused
redis启动报错-Could not connect to Redis at 127.0.0.1:6379: Connection refused
|
SQL HIVE
hive创建表并指定字段分隔符
hive创建表并指定字段分隔符
|
Java
《Java开发手册(嵩山版)灵魂15问》下载
《〈Java开发手册(嵩山版)〉灵魂15问》重磅来袭!“一线大厂如何用Java” 解读再升级,千万阅读量博主深究Java规约背后的原理。
288 0
《Java开发手册(嵩山版)灵魂15问》下载
|
Oracle 关系型数据库 数据库
navicat提示oracle library is not loaded
navicat提示oracle library is not loaded
2885 0
navicat提示oracle library is not loaded
@PathVariable注解的作用
1.@PathVariable注解的作用 在SpringMVC中可以使用占位符进行参数绑定,地址/list/1/3可以写成/list/{page}/{limit},占位符{page}和{limit}对应的值就是1和3。在业务方法中我们可以使用@PathVariable注解进行占位符的匹配获取工作。
|
存储 算法 安全
JAVA的MD5加密与解密
昨天是老同学生日 00点后就是我生日了 搞的我都好那个了 两点多开了房睡觉 快三点多才睡着 今天做程序 同事说要把页面上的绑定信息加个密 好久没有写过加密算法了 都生疏了 找了段MD5算法就加了下密。。
1251 1
我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
1074 0
|
存储 索引
IL指令详细表
名称 说明 Add 将两个值相加并将结果推送到计算堆栈上。 Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上。 Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且将结果推送到计算堆栈上。
1000 0
|
HTML5 移动开发
使用两个zTree,调用一个onCheck函数,分别展开不同的节点
问题:在一个页面使用了两个zTree,用选项卡区分,现在要点击节点展示相应的子节点,两个zTree怎么区分? 解决方案: 1 function onCheck(e, treeId, treeNode) { 2 console.
882 0

相关产品

  • 云迁移中心