DataReaderExtensions.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlTypes;
  5. using System.Linq;
  6. using System.Text;
  7. using Ant.Utility;
  8. namespace Ant.Extensions
  9. {
  10. public static class DataReaderExtensions
  11. {
  12. public static short GetInt16(IDataReader reader, int ordinal)
  13. {
  14. return reader.GetInt16(ordinal);
  15. }
  16. public static short? GetInt16_Nullable(this IDataReader reader, int ordinal)
  17. {
  18. if (reader.IsDBNull(ordinal))
  19. {
  20. return null;
  21. }
  22. return reader.GetInt16(ordinal);
  23. }
  24. public static int GetInt32(IDataReader reader, int ordinal)
  25. {
  26. return reader.GetInt32(ordinal);
  27. }
  28. public static int? GetInt32_Nullable(this IDataReader reader, int ordinal)
  29. {
  30. if (reader.IsDBNull(ordinal))
  31. {
  32. return null;
  33. }
  34. return reader.GetInt32(ordinal);
  35. }
  36. public static long GetInt64(IDataReader reader, int ordinal)
  37. {
  38. return reader.GetInt64(ordinal);
  39. }
  40. public static long? GetInt64_Nullable(this IDataReader reader, int ordinal)
  41. {
  42. if (reader.IsDBNull(ordinal))
  43. {
  44. return null;
  45. }
  46. return reader.GetInt64(ordinal);
  47. }
  48. public static decimal GetDecimal(IDataReader reader, int ordinal)
  49. {
  50. return reader.GetDecimal(ordinal);
  51. }
  52. public static decimal? GetDecimal_Nullable(this IDataReader reader, int ordinal)
  53. {
  54. if (reader.IsDBNull(ordinal))
  55. {
  56. return null;
  57. }
  58. return reader.GetDecimal(ordinal);
  59. }
  60. public static double GetDouble(IDataReader reader, int ordinal)
  61. {
  62. return reader.GetDouble(ordinal);
  63. }
  64. public static double? GetDouble_Nullable(this IDataReader reader, int ordinal)
  65. {
  66. if (reader.IsDBNull(ordinal))
  67. {
  68. return null;
  69. }
  70. return reader.GetDouble(ordinal);
  71. }
  72. public static float GetFloat(IDataReader reader, int ordinal)
  73. {
  74. return reader.GetFloat(ordinal);
  75. }
  76. public static float? GetFloat_Nullable(this IDataReader reader, int ordinal)
  77. {
  78. if (reader.IsDBNull(ordinal))
  79. {
  80. return null;
  81. }
  82. return reader.GetFloat(ordinal);
  83. }
  84. public static bool GetBoolean(IDataReader reader, int ordinal)
  85. {
  86. return reader.GetBoolean(ordinal);
  87. }
  88. public static bool? GetBoolean_Nullable(this IDataReader reader, int ordinal)
  89. {
  90. if (reader.IsDBNull(ordinal))
  91. {
  92. return null;
  93. }
  94. return reader.GetBoolean(ordinal);
  95. }
  96. public static DateTime GetDateTime(IDataReader reader, int ordinal)
  97. {
  98. return reader.GetDateTime(ordinal);
  99. }
  100. public static DateTime? GetDateTime_Nullable(this IDataReader reader, int ordinal)
  101. {
  102. if (reader.IsDBNull(ordinal))
  103. {
  104. return null;
  105. }
  106. return reader.GetDateTime(ordinal);
  107. }
  108. public static Guid GetGuid(IDataReader reader, int ordinal)
  109. {
  110. return reader.GetGuid(ordinal);
  111. }
  112. public static Guid? GetGuid_Nullable(this IDataReader reader, int ordinal)
  113. {
  114. if (reader.IsDBNull(ordinal))
  115. {
  116. return null;
  117. }
  118. return reader.GetGuid(ordinal);
  119. }
  120. public static byte GetByte(IDataReader reader, int ordinal)
  121. {
  122. return reader.GetByte(ordinal);
  123. }
  124. public static byte? GetByte_Nullable(this IDataReader reader, int ordinal)
  125. {
  126. if (reader.IsDBNull(ordinal))
  127. {
  128. return null;
  129. }
  130. return reader.GetByte(ordinal);
  131. }
  132. public static char GetChar(IDataReader reader, int ordinal)
  133. {
  134. return reader.GetChar(ordinal);
  135. }
  136. public static char? GetChar_Nullable(this IDataReader reader, int ordinal)
  137. {
  138. if (reader.IsDBNull(ordinal))
  139. {
  140. return null;
  141. }
  142. return reader.GetChar(ordinal);
  143. }
  144. public static string GetString(IDataReader reader, int ordinal)
  145. {
  146. object o = reader.GetValue(ordinal);
  147. if (o == DBNull.Value)
  148. {
  149. return null;
  150. }
  151. return (string)o;
  152. }
  153. public static object GetValue(IDataReader reader, int ordinal)
  154. {
  155. object o = reader.GetValue(ordinal);
  156. if (o == DBNull.Value)
  157. {
  158. return null;
  159. }
  160. return o;
  161. }
  162. public static T GetEnum<T>(this IDataReader reader, int ordinal) where T : struct
  163. {
  164. int value = reader.GetInt32(ordinal);
  165. T t = (T)Enum.ToObject(typeof(T), value);
  166. return t;
  167. }
  168. public static T? GetEnum_Nullable<T>(this IDataReader reader, int ordinal) where T : struct
  169. {
  170. if (reader.IsDBNull(ordinal))
  171. {
  172. return null;
  173. }
  174. int value = reader.GetInt32(ordinal);
  175. T t = (T)Enum.ToObject(typeof(T), value);
  176. return t;
  177. }
  178. public static T GetTValue<T>(this IDataReader reader, int ordinal)
  179. {
  180. T obj = default(T);
  181. object val = reader.GetValue(ordinal);
  182. if (val == DBNull.Value)
  183. {
  184. val = null;
  185. }
  186. try
  187. {
  188. return (T)val;
  189. }
  190. catch (NullReferenceException)
  191. {
  192. throw new Exception("The column value could not be null");
  193. }
  194. }
  195. public static T? GetTValue_Nullable<T>(this IDataReader reader, int ordinal) where T : struct
  196. {
  197. object val = reader.GetValue(ordinal);
  198. if (val == DBNull.Value)
  199. {
  200. return null;
  201. }
  202. return new Nullable<T>((T)val);
  203. }
  204. }
  205. }