纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.3 KiB

3 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace NFine.Data.Extensions
  10. {
  11. public class DatabaseReader
  12. {
  13. /// <summary>
  14. /// 将IDataReader转换为DataTable
  15. /// </summary>
  16. /// <param name="dr"></param>
  17. /// <returns></returns>
  18. public static DataTable ReaderToDataTable(IDataReader dr)
  19. {
  20. using (dr)
  21. {
  22. DataTable objDataTable = new DataTable("Table");
  23. int intFieldCount = dr.FieldCount;
  24. for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
  25. {
  26. // 2017-2-21 列名被改成小写
  27. //objDataTable.Columns.Add(dr.GetName(intCounter).ToLower(), dr.GetFieldType(intCounter));
  28. objDataTable.Columns.Add(dr.GetName(intCounter), dr.GetFieldType(intCounter));
  29. }
  30. objDataTable.BeginLoadData();
  31. object[] objValues = new object[intFieldCount];
  32. while (dr.Read())
  33. {
  34. dr.GetValues(objValues);
  35. objDataTable.LoadDataRow(objValues, true);
  36. }
  37. dr.Close();
  38. objDataTable.EndLoadData();
  39. return objDataTable;
  40. }
  41. }
  42. /// <summary>
  43. /// 将IDataReader转换为 集合
  44. /// </summary>
  45. /// <typeparam name="T"></typeparam>
  46. /// <param name="dr"></param>
  47. /// <returns></returns>
  48. public static List<T> ReaderToList<T>(IDataReader dr)
  49. {
  50. using (dr)
  51. {
  52. List<string> field = new List<string>(dr.FieldCount);
  53. for (int i = 0; i < dr.FieldCount; i++)
  54. {
  55. //field.Add(dr.GetName(i).ToLower());
  56. field.Add(dr.GetName(i));
  57. }
  58. List<T> list = new List<T>();
  59. while (dr.Read())
  60. {
  61. T model = Activator.CreateInstance<T>();
  62. foreach (PropertyInfo property in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
  63. {
  64. //if (field.Contains(property.Name.ToLower()))
  65. if (field.Contains(property.Name))
  66. {
  67. if (!IsNullOrDBNull(dr[property.Name]))
  68. {
  69. property.SetValue(model, HackType(dr[property.Name], property.PropertyType), null);
  70. }
  71. }
  72. }
  73. list.Add(model);
  74. }
  75. dr.Close();
  76. return list;
  77. }
  78. }
  79. /// <summary>
  80. /// 将IDataReader转换为 实体类
  81. /// </summary>
  82. /// <typeparam name="T"></typeparam>
  83. /// <param name="dr"></param>
  84. /// <returns></returns>
  85. public static T ReaderToModel<T>(IDataReader dr)
  86. {
  87. using (dr)
  88. {
  89. T model = Activator.CreateInstance<T>();
  90. while (dr.Read())
  91. {
  92. foreach (PropertyInfo pi in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
  93. {
  94. if (!IsNullOrDBNull(dr[pi.Name]))
  95. {
  96. pi.SetValue(model, HackType(dr[pi.Name], pi.PropertyType), null);
  97. }
  98. }
  99. }
  100. dr.Close();
  101. return model;
  102. }
  103. }
  104. /// <summary>
  105. /// 将IDataReader转换为 哈希表
  106. /// </summary>
  107. /// <param name="dr"></param>
  108. /// <returns></returns>
  109. public static Hashtable ReaderToHashtable(IDataReader dr)
  110. {
  111. using (dr)
  112. {
  113. Hashtable ht = new Hashtable();
  114. while (dr.Read())
  115. {
  116. for (int i = 0; i < dr.FieldCount; i++)
  117. {
  118. //string strfield = dr.GetName(i).ToLower();
  119. string strfield = dr.GetName(i);
  120. ht[strfield] = dr[strfield];
  121. }
  122. }
  123. dr.Close();
  124. return ht;
  125. }
  126. }
  127. //这个类对可空类型进行判断转换,要不然会报错
  128. public static object HackType(object value, Type conversionType)
  129. {
  130. if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
  131. {
  132. if (value == null)
  133. return null;
  134. System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
  135. conversionType = nullableConverter.UnderlyingType;
  136. }
  137. return Convert.ChangeType(value, conversionType);
  138. }
  139. public static bool IsNullOrDBNull(object obj)
  140. {
  141. return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
  142. }
  143. }
  144. }