DataConvert.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*******************************************************************************
  2. * Copyright(c) 2012 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:Convert.cs
  5. * 2.功能描述:系统各种数据类型之间转换
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 欧阳涛 2012/07/04 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Data;
  13. using System.Reflection;
  14. using System.Windows.Forms;
  15. namespace Dongke.IBOSS.PRD.Basics.Library
  16. {
  17. /// <summary>
  18. /// 系统各种数据类型之间转换
  19. /// </summary>
  20. static public class DataConvert
  21. {
  22. #region ToDataTable()
  23. /// <summary>
  24. /// 将控件的数据源转成DataTable类型
  25. /// </summary>
  26. /// <param name="source">数据源</param>
  27. /// <param name="member">绑定的列成员</param>
  28. /// <returns>转换后的DataTable数据源</returns>
  29. public static DataTable ToDataTable(object source, string member)
  30. {
  31. BindingSource bindingSource = source as System.Windows.Forms.BindingSource;
  32. if (bindingSource != null)
  33. {
  34. if (string.IsNullOrEmpty(bindingSource.DataMember) == false)
  35. {
  36. member = bindingSource.DataMember;
  37. }
  38. source = bindingSource.DataSource;
  39. while (true)
  40. {
  41. if (source is System.Windows.Forms.BindingSource)
  42. {
  43. source = ((System.Windows.Forms.BindingSource)source).DataSource;
  44. if (source is DataSet)
  45. {
  46. return ((DataSet)source).Relations[member].ChildTable;
  47. }
  48. }
  49. else
  50. {
  51. break;
  52. }
  53. }
  54. }
  55. if (source is DataSet)
  56. {
  57. DataSet set = (DataSet)source;
  58. DataTable table = set.Tables[member];
  59. if (table != null)
  60. {
  61. return table;
  62. }
  63. string[] splits = member.Split('.');
  64. if (splits.Length == 2)
  65. {
  66. table = set.Tables[splits[0]];
  67. DataRelation relation = set.Relations[splits[1]];
  68. if (relation.ParentTable == table)
  69. {
  70. return relation.ChildTable;
  71. }
  72. }
  73. return null;
  74. }
  75. else if (source is DataTable)
  76. {
  77. return (DataTable)source;
  78. }
  79. else if (source is DataView)
  80. {
  81. return ((DataView)source).Table;
  82. }
  83. else
  84. {
  85. return null;
  86. }
  87. }
  88. #endregion
  89. #region ToObjectArray<T>()
  90. public static object[] ToObjectArray<T>(T[] array)
  91. {
  92. if (array == null)
  93. {
  94. return null;
  95. }
  96. List<object> list = new List<object>();
  97. foreach (object obj in array)
  98. {
  99. list.Add(obj);
  100. }
  101. return list.ToArray();
  102. }
  103. public static T[] ToTArray<T>(object[] array)
  104. {
  105. if (array == null)
  106. {
  107. return null;
  108. }
  109. List<T> list = new List<T>();
  110. foreach (T obj in array)
  111. {
  112. list.Add(obj);
  113. }
  114. return list.ToArray();
  115. }
  116. #endregion
  117. /// <summary>
  118. /// 把Table转换成List
  119. /// </summary>
  120. /// <typeparam name="T">指定转换的类型</typeparam>
  121. /// <param name="pDt">DataTable</param>
  122. /// <returns></returns>
  123. public static List<T> TableConvertToObject<T>(DataTable pDt)
  124. {
  125. if (pDt == null)
  126. {
  127. return default(List<T>);
  128. }
  129. List<T> lst = new List<T>();
  130. for (int i = 0; i < pDt.Rows.Count; i++)
  131. {
  132. lst.Add(DataRowConvertToObject<T>(pDt.Rows[i]));
  133. }
  134. return lst;
  135. }
  136. /// <summary>
  137. /// 把DataRow转换成实体类Model
  138. /// </summary>
  139. /// <typeparam name="T">类型</typeparam>
  140. /// <param name="row">DataRow</param>
  141. /// <returns></returns>
  142. public static T DataRowConvertToObject<T>(DataRow row)
  143. {
  144. Type t = typeof(T);
  145. object obj = Activator.CreateInstance(t);
  146. PropertyInfo[] property_infos = t.GetProperties();
  147. if (row != null)
  148. {
  149. foreach (PropertyInfo property_info in property_infos)
  150. {
  151. string property_name = property_info.Name;
  152. try
  153. {
  154. if (row[property_name] != null)
  155. {
  156. string fieldType = property_info.PropertyType.FullName;
  157. property_info.SetValue(obj, GetType(row[property_name], fieldType), null);
  158. }
  159. }
  160. catch
  161. {
  162. }
  163. }
  164. }
  165. return (T)obj;
  166. }
  167. /// <summary>
  168. /// 把a的所有属性值,赋值给b的对应属性。
  169. /// </summary>
  170. /// <param name="a">对象a。</param>
  171. /// <param name="b">对象b。</param>
  172. public static void Convert(object a, object b)
  173. {
  174. List<Type> listType = new List<Type> {
  175. typeof(Boolean),
  176. typeof(Boolean?),
  177. typeof(Int64),
  178. typeof(Int64?),
  179. typeof(Int32),
  180. typeof(Int32?),
  181. typeof(String),
  182. typeof(Double),
  183. typeof(Double?),
  184. typeof(DateTime),
  185. typeof(DateTime?),
  186. typeof(Byte[]),
  187. typeof(Guid),
  188. typeof(Guid?),
  189. typeof(Decimal),
  190. typeof(Decimal?),
  191. typeof(Char?),
  192. typeof(Dictionary<string, string>),
  193. typeof(DataTable),
  194. typeof(DataSet)
  195. };
  196. if (a == null || b == null)
  197. {
  198. return;
  199. }
  200. //循环所有属性,从a中取值并赋给b的对应属性。
  201. foreach (var item in a.GetType().GetProperties())
  202. {
  203. if (listType.Contains(item.PropertyType))
  204. {
  205. object value = item.GetValue(a, null);
  206. var property = b.GetType().GetProperty(item.Name);
  207. if (property != null)
  208. {
  209. property.SetValue(b, value, null);
  210. }
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 把a的所有属性值,赋值给b的对应属性。
  216. /// 除了Guid类型外
  217. /// </summary>
  218. /// <param name="a">对象a。</param>
  219. /// <param name="b">对象b。</param>
  220. public static void ConvertWithoutGuid(object a, object b)
  221. {
  222. List<Type> listType = new List<Type> {
  223. typeof(Boolean),
  224. typeof(Boolean?),
  225. typeof(Int32),
  226. typeof(Int32?),
  227. typeof(String),
  228. typeof(Double),
  229. typeof(Double?),
  230. typeof(DateTime),
  231. typeof(DateTime?),
  232. typeof(Byte[]),
  233. typeof(Decimal),
  234. typeof(Decimal?),
  235. //typeof(Guid),
  236. typeof(Dictionary<string, string>)
  237. };
  238. if (a == null || b == null)
  239. {
  240. return;
  241. }
  242. //循环所有属性,从a中取值并赋给b的对应属性。
  243. foreach (var item in a.GetType().GetProperties())
  244. {
  245. if (listType.Contains(item.PropertyType))
  246. {
  247. object value = item.GetValue(a, null);
  248. var property = b.GetType().GetProperty(item.Name);
  249. if (property != null)
  250. {
  251. property.SetValue(b, value, null);
  252. }
  253. }
  254. }
  255. }
  256. /// <summary>
  257. /// 把a的所有属性值,赋值给b的对应属性。
  258. /// </summary>
  259. /// <param name="a">对象a。</param>
  260. /// <param name="b">对象b。</param>
  261. public static void ConvertWithoutTimeStamp(object a, object b)
  262. {
  263. List<Type> listType = new List<Type> {
  264. typeof(Boolean),
  265. typeof(Boolean?),
  266. typeof(Int32),
  267. typeof(Int32?),
  268. typeof(String),
  269. typeof(Double),
  270. typeof(Double?),
  271. typeof(DateTime),
  272. typeof(DateTime?),
  273. typeof(Byte[]),
  274. typeof(Guid),
  275. typeof(Guid?),
  276. typeof(Decimal),
  277. typeof(Decimal?),
  278. typeof(Dictionary<string, string>)
  279. };
  280. if (a == null || b == null)
  281. {
  282. return;
  283. }
  284. //循环所有属性,从a中取值并赋给b的对应属性。
  285. foreach (var item in a.GetType().GetProperties())
  286. {
  287. if (item.Name.CompareTo("timestamp") == 0)
  288. { continue; }
  289. if (listType.Contains(item.PropertyType))
  290. {
  291. object value = item.GetValue(a, null);
  292. var property = b.GetType().GetProperty(item.Name);
  293. if (property != null)
  294. {
  295. property.SetValue(b, value, null);
  296. }
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// 复制除某个字段以外的所有字段
  302. /// </summary>
  303. /// <param name="a">对象a。</param>
  304. /// <param name="b">对象b。</param>
  305. /// <param name="key">字段</param>
  306. public static void ConvertWithoutKey(object a, object b, string key)
  307. {
  308. List<Type> listType = new List<Type> {
  309. typeof(Boolean),
  310. typeof(Boolean?),
  311. typeof(Int32),
  312. typeof(Int32?),
  313. typeof(String),
  314. typeof(Double),
  315. typeof(Double?),
  316. typeof(DateTime),
  317. typeof(DateTime?),
  318. typeof(Byte[]),
  319. typeof(Guid),
  320. typeof(Guid?),
  321. typeof(Decimal),
  322. typeof(Decimal?),
  323. typeof(Dictionary<string, string>)
  324. };
  325. if (a == null || b == null)
  326. {
  327. return;
  328. }
  329. //循环所有属性,从a中取值并赋给b的对应属性。
  330. foreach (var item in a.GetType().GetProperties())
  331. {
  332. if (item.Name.CompareTo(key) == 0)
  333. { continue; }
  334. if (listType.Contains(item.PropertyType))
  335. {
  336. object value = item.GetValue(a, null);
  337. var property = b.GetType().GetProperty(item.Name);
  338. if (property != null)
  339. {
  340. property.SetValue(b, value, null);
  341. }
  342. }
  343. }
  344. }
  345. #region 返回类型格式
  346. /// <summary>
  347. /// 返回类型格式
  348. /// </summary>
  349. /// <param name="obj">字段值</param>
  350. /// <param name="type">字段类型</param>
  351. /// <returns></returns>
  352. private static dynamic GetType(object obj, string type)
  353. {
  354. if (obj == System.DBNull.Value)
  355. {
  356. return null;
  357. }
  358. if (type.ToLower().Contains("string"))
  359. {
  360. return obj.ToString() == "" ? null : obj.ToString();
  361. }
  362. else if (type.ToLower().Contains("int"))
  363. {
  364. return int.Parse(obj.ToString());
  365. }
  366. else if (type.ToLower().Contains("datetime"))
  367. {
  368. return DateTime.Parse(obj.ToString());
  369. }
  370. else if (type.ToLower().Contains("decimal"))
  371. {
  372. return decimal.Parse(obj.ToString());
  373. }
  374. else if (type.ToLower().Contains("double"))
  375. {
  376. return double.Parse(obj.ToString());
  377. }
  378. else
  379. {
  380. return obj;
  381. }
  382. }
  383. #endregion
  384. /// <summary>
  385. /// 将传入的List 转换为 SQL 中的IN条件 例如 1,2,3,4
  386. /// </summary>
  387. /// <param name="pListKey">List数据</param>
  388. /// <returns></returns>
  389. public static string ConvertListToSqlInWhere(int[] pListKey)
  390. {
  391. System.Text.StringBuilder sbWhere = new System.Text.StringBuilder();
  392. sbWhere.Append("(");
  393. foreach (int itemKey in pListKey)
  394. {
  395. sbWhere.Append(itemKey + ",");
  396. }
  397. string strWhere = sbWhere.ToString();
  398. if (strWhere.Length >= 1)
  399. {
  400. strWhere = strWhere.Substring(0, strWhere.Length - 1);
  401. }
  402. if (strWhere.Length > 0)
  403. {
  404. strWhere = strWhere + ")";
  405. }
  406. else
  407. {
  408. strWhere = string.Empty;
  409. }
  410. return strWhere;
  411. }
  412. /// <summary>
  413. /// 将类集合转换为DataTable
  414. /// </summary>
  415. /// <typeparam name="T">类</typeparam>
  416. /// <param name="objects">类集合</param>
  417. /// <returns></returns>
  418. public static DataTable ObjectConvertToTable<T>(T[] objects)
  419. {
  420. if (objects == null || objects.Length <= 0)
  421. {
  422. return new DataTable();
  423. }
  424. DataTable dTable = new DataTable("Table1");
  425. Type t = typeof(T);
  426. PropertyInfo[] property_infos = t.GetProperties();
  427. foreach (PropertyInfo property_info in property_infos)
  428. {
  429. string property_name = property_info.Name;
  430. dTable.Columns.Add(property_name);
  431. }
  432. foreach (T itemT in objects)
  433. {
  434. DataRow newRowObject = dTable.NewRow();
  435. foreach (PropertyInfo property_info in property_infos)
  436. {
  437. string property_name = property_info.Name;
  438. var property_value = property_info.GetValue(itemT, null);
  439. newRowObject[property_name] = property_value;
  440. }
  441. dTable.Rows.Add(newRowObject);
  442. }
  443. return dTable;
  444. }
  445. #region 10 36 转换
  446. private const string X36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  447. /// <summary>
  448. /// 10进制转换成36进制
  449. /// </summary>
  450. /// <param name="val"></param>
  451. /// <returns></returns>
  452. public static string Convert10To36(long val, int length = 8)
  453. {
  454. string result = "";
  455. while (val >= 36)
  456. {
  457. int valtmp = (int)(val % 36);
  458. //result = (valtmp >= 0 && valtmp <= 9 ? (char)(valtmp + 48) : (char)(valtmp + 55)) + result;
  459. result = X36[valtmp] + result;
  460. val /= 36;
  461. }
  462. if (val >= 0)
  463. {
  464. //result = (val >= 0 && val <= 9 ? (char)(val + 48) : (char)(val + 55)) + result;
  465. result = X36[(int)val] + result;
  466. }
  467. if (result.Length < length)
  468. {
  469. result = result.PadLeft(length, '0');
  470. }
  471. else if (result.Length > length)
  472. {
  473. result = result.Substring(result.Length - length);
  474. }
  475. return result;
  476. }
  477. /// <summary>
  478. /// 36进制转换成10进制
  479. /// </summary>
  480. /// <param name="str"></param>
  481. /// <returns></returns>
  482. public static long Convert36To10(string str)
  483. {
  484. long result = 0;
  485. int len = str.Length;
  486. for (int i = len; i > 0; i--)
  487. {
  488. result += X36.IndexOf(str[i - 1]) * System.Convert.ToInt64(Math.Pow(36, len - i));
  489. }
  490. return result;
  491. }
  492. #endregion
  493. }
  494. }