DatabaseReader.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. namespace Ant.ORM
  10. {
  11. public class DatabaseReader
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. /// <param name="value"></param>
  17. /// <param name="conversionType"></param>
  18. /// <returns></returns>
  19. public static object HackType(object value, Type conversionType)
  20. {
  21. if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
  22. {
  23. if (value == null)
  24. {
  25. return null;
  26. }
  27. NullableConverter converter = new NullableConverter(conversionType);
  28. conversionType = converter.UnderlyingType;
  29. }
  30. return Convert.ChangeType(value, conversionType);
  31. }
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. /// <param name="obj"></param>
  36. /// <returns></returns>
  37. public static bool IsNullOrDBNull(object obj)
  38. {
  39. return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString()));
  40. }
  41. /// <summary>
  42. /// Reader转换成DataTable
  43. /// </summary>
  44. /// <param name="reader"></param>
  45. /// <returns></returns>
  46. public static DataTable ReaderToDataTable(IDataReader reader)
  47. {
  48. DataTable table = new DataTable("Table");
  49. int fieldCount = reader.FieldCount;
  50. for (int i = 0; i < fieldCount; i++)
  51. {
  52. table.Columns.Add(reader.GetName(i).ToLower(), reader.GetFieldType(i));
  53. }
  54. table.BeginLoadData();
  55. object[] values = new object[fieldCount];
  56. while (reader.Read())
  57. {
  58. reader.GetValues(values);
  59. table.LoadDataRow(values, true);
  60. }
  61. reader.Close();
  62. table.EndLoadData();
  63. return table;
  64. }
  65. /// <summary>
  66. ///
  67. /// </summary>
  68. /// <param name="dr"></param>
  69. /// <returns></returns>
  70. public static Hashtable ReaderToHashtable(IDataReader dr)
  71. {
  72. Hashtable hashtable = new Hashtable();
  73. while (dr.Read())
  74. {
  75. for (int i = 0; i < dr.FieldCount; i++)
  76. {
  77. string str = dr.GetName(i).ToLower();
  78. hashtable[str] = dr[str];
  79. }
  80. }
  81. return hashtable;
  82. }
  83. /// <summary>
  84. ///
  85. /// </summary>
  86. /// <typeparam name="T"></typeparam>
  87. /// <param name="dr"></param>
  88. /// <returns></returns>
  89. public static List<T> ReaderToList<T>(IDataReader dr)
  90. {
  91. using (dr)
  92. {
  93. List<string> list = new List<string>(dr.FieldCount);
  94. for (int i = 0; i < dr.FieldCount; i++)
  95. {
  96. list.Add(dr.GetName(i).ToLower());
  97. }
  98. List<T> list2 = new List<T>();
  99. while (dr.Read())
  100. {
  101. T local = Activator.CreateInstance<T>();
  102. foreach (PropertyInfo info in local.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
  103. {
  104. if (list.Contains(info.Name.ToLower()) && !IsNullOrDBNull(dr[info.Name]))
  105. {
  106. info.SetValue(local, HackType(dr[info.Name], info.PropertyType), null);
  107. }
  108. }
  109. list2.Add(local);
  110. }
  111. return list2;
  112. }
  113. }
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. /// <typeparam name="T"></typeparam>
  118. /// <param name="dr"></param>
  119. /// <returns></returns>
  120. public static T ReaderToModel<T>(IDataReader dr)
  121. {
  122. T local = Activator.CreateInstance<T>();
  123. while (dr.Read())
  124. {
  125. foreach (PropertyInfo info in local.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
  126. {
  127. if (!IsNullOrDBNull(dr[info.Name]))
  128. {
  129. info.SetValue(local, HackType(dr[info.Name], info.PropertyType), null);
  130. }
  131. }
  132. }
  133. return local;
  134. }
  135. }
  136. }