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.
 
 
 
 
 
 

252 lines
8.9 KiB

  1. using Dapper;
  2. using Learun.Util;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.ComponentModel.DataAnnotations.Schema;
  8. using System.Data;
  9. using System.Reflection;
  10. using System.Text;
  11. namespace Learun.DataBase
  12. {
  13. /// <summary>
  14. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  15. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  16. /// 创建人:陈彬彬
  17. /// 日 期:2017.03.04
  18. /// 描 述:数据访问扩展
  19. /// </summary>
  20. public static class SqlHelper
  21. {
  22. #region 转换扩展类
  23. /// <summary>
  24. /// 将IDataReader转换为DataTable
  25. /// </summary>
  26. /// <param name="dr">数据读取接口</param>
  27. /// <returns></returns>
  28. public static DataTable IDataReaderToDataTable(IDataReader reader)
  29. {
  30. using (reader)
  31. {
  32. DataTable objDataTable = new DataTable("Table");
  33. int intFieldCount = reader.FieldCount;
  34. for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
  35. {
  36. objDataTable.Columns.Add(reader.GetName(intCounter).ToLower(), reader.GetFieldType(intCounter));
  37. }
  38. objDataTable.BeginLoadData();
  39. object[] objValues = new object[intFieldCount];
  40. while (reader.Read())
  41. {
  42. reader.GetValues(objValues);
  43. objDataTable.LoadDataRow(objValues, true);
  44. }
  45. reader.Close();
  46. reader.Dispose();
  47. objDataTable.EndLoadData();
  48. return objDataTable;
  49. }
  50. }
  51. /// <summary>
  52. /// 获取实体类键值(缓存)
  53. /// </summary>
  54. /// <typeparam name="T">类型</typeparam>
  55. /// <param name="entity">实体对象</param>
  56. /// <returns></returns>
  57. public static Hashtable GetPropertyInfo<T>(T entity)
  58. {
  59. Type type = entity.GetType();
  60. //object CacheEntity = CacheHelper.GetCache("CacheEntity_" + EntityAttribute.GetEntityTable<T>());
  61. object CacheEntity = null;
  62. if (CacheEntity == null)
  63. {
  64. Hashtable ht = new Hashtable();
  65. PropertyInfo[] props = type.GetProperties();
  66. foreach (PropertyInfo prop in props)
  67. {
  68. bool flag = true;
  69. foreach (Attribute attr in prop.GetCustomAttributes(true))
  70. {
  71. NotMappedAttribute notMapped = attr as NotMappedAttribute;
  72. if (notMapped != null)
  73. {
  74. flag = false;
  75. break;
  76. }
  77. }
  78. if (flag)
  79. {
  80. string name = prop.Name;
  81. object value = prop.GetValue(entity, null);
  82. ht[name] = value;
  83. }
  84. }
  85. //CacheHelper.SetCache("CacheEntity_" + EntityAttribute.GetEntityTable<T>(), ht);
  86. return ht;
  87. }
  88. else
  89. {
  90. return (Hashtable)CacheEntity;
  91. }
  92. }
  93. /// <summary>
  94. /// 将json对象转化成Dapper可认的参数
  95. /// </summary>
  96. /// <param name="jObject">json对象</param>
  97. /// <returns></returns>
  98. public static DynamicParameters JObjectToParameter(JObject jObject)
  99. {
  100. try
  101. {
  102. var args = new DynamicParameters(new { });
  103. foreach (var item in jObject)
  104. {
  105. args.Add(item.Key.ToString(), item.Value.ToString());
  106. }
  107. return args;
  108. }
  109. catch (Exception)
  110. {
  111. throw;
  112. }
  113. }
  114. /// <summary>
  115. /// 将对象转化成Dapper可认的参数
  116. /// </summary>
  117. /// <param name="fieldValueParams">对象</param>
  118. /// <returns></returns>
  119. public static DynamicParameters FieldValueParamToParameter(List<FieldValueParam> fieldValueParams)
  120. {
  121. try
  122. {
  123. var args = new DynamicParameters(new { });
  124. foreach (var item in fieldValueParams)
  125. {
  126. args.Add(item.name, item.value, (DbType)item.type);
  127. }
  128. return args;
  129. }
  130. catch (Exception)
  131. {
  132. throw;
  133. }
  134. }
  135. #endregion
  136. #region 分页语句
  137. /// <summary>
  138. /// sql分页语句
  139. /// </summary>
  140. /// <param name="strSql">sql语句</param>
  141. /// <param name="orderField">排序字段</param>
  142. /// <param name="isAsc">排序类型</param>
  143. /// <param name="pageSize">每页数据条数</param>
  144. /// <param name="pageIndex">页码</param>
  145. /// <returns></returns>
  146. public static StringBuilder SqlPageSql(string strSql, string orderField, bool isAsc, int pageSize, int pageIndex)
  147. {
  148. StringBuilder sb = new StringBuilder();
  149. if (pageIndex == 0)
  150. {
  151. pageIndex = 1;
  152. }
  153. int num = (pageIndex - 1) * pageSize;
  154. int num1 = (pageIndex) * pageSize;
  155. string OrderBy = "";
  156. if (!string.IsNullOrEmpty(orderField))
  157. {
  158. if (orderField.ToUpper().IndexOf("ASC") + orderField.ToUpper().IndexOf("DESC") > 0)
  159. {
  160. OrderBy = " Order By " + orderField;
  161. }
  162. else
  163. {
  164. OrderBy = " Order By " + orderField + " " + (isAsc ? "ASC" : "DESC");
  165. }
  166. }
  167. else
  168. {
  169. OrderBy = "order by (select 0)";
  170. }
  171. sb.Append("Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
  172. sb.Append(" As rowNum, * From (" + strSql + ") T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
  173. return sb;
  174. }
  175. /// <summary>
  176. /// oracle分页语句
  177. /// </summary>
  178. /// <param name="strSql">sql语句</param>
  179. /// <param name="orderField">排序字段</param>
  180. /// <param name="isAsc">排序类型</param>
  181. /// <param name="pageSize">每页数据条数</param>
  182. /// <param name="pageIndex">页码</param>
  183. /// <returns></returns>
  184. public static StringBuilder OraclePageSql(string strSql, string orderField, bool isAsc, int pageSize, int pageIndex)
  185. {
  186. StringBuilder sb = new StringBuilder();
  187. if (pageIndex == 0)
  188. {
  189. pageIndex = 1;
  190. }
  191. int num = (pageIndex - 1) * pageSize;
  192. int num1 = (pageIndex) * pageSize;
  193. string OrderBy = "";
  194. if (!string.IsNullOrEmpty(orderField))
  195. {
  196. if (orderField.ToUpper().IndexOf("ASC") + orderField.ToUpper().IndexOf("DESC") > 0)
  197. {
  198. OrderBy = " Order By " + orderField;
  199. }
  200. else
  201. {
  202. OrderBy = " Order By " + orderField + " " + (isAsc ? "ASC" : "DESC");
  203. }
  204. }
  205. sb.Append("Select * From (Select ROWNUM lrrn,");
  206. sb.Append(" T.* From (" + strSql + OrderBy + ") T ) N Where lrrn > " + num + " And lrrn <= " + num1 + "");
  207. return sb;
  208. }
  209. /// <summary>
  210. /// mysql分页语句
  211. /// </summary>
  212. /// <param name="strSql">sql语句</param>
  213. /// <param name="orderField">排序字段</param>
  214. /// <param name="isAsc">排序类型</param>
  215. /// <param name="pageSize">每页数据条数</param>
  216. /// <param name="pageIndex">页码</param>
  217. /// <returns></returns>
  218. public static StringBuilder MySqlPageSql(string strSql, string orderField, bool isAsc, int pageSize, int pageIndex)
  219. {
  220. StringBuilder sb = new StringBuilder();
  221. if (pageIndex == 0)
  222. {
  223. pageIndex = 1;
  224. }
  225. int num = (pageIndex - 1) * pageSize;
  226. string OrderBy = "";
  227. if (!string.IsNullOrEmpty(orderField))
  228. {
  229. if (orderField.ToUpper().IndexOf("ASC") + orderField.ToUpper().IndexOf("DESC") > 0)
  230. {
  231. OrderBy = " Order By " + orderField;
  232. }
  233. else
  234. {
  235. OrderBy = " Order By " + orderField + " " + (isAsc ? "ASC" : "DESC");
  236. }
  237. }
  238. sb.Append(strSql + OrderBy);
  239. sb.Append(" limit " + num + "," + pageSize + "");
  240. return sb;
  241. }
  242. #endregion
  243. }
  244. }