SugarClient.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using SqlSugar;
  2. using System;
  3. using System.Configuration;
  4. using System.Data.SqlClient;
  5. using System.Runtime.InteropServices;
  6. namespace WCS_Client
  7. {
  8. public class SugarClient
  9. {
  10. internal static string _connectionString = ConfigurationManager.ConnectionStrings["OracleDatabase"].ConnectionString;
  11. //创建sqlserver连接对象
  12. public static SqlConnection GetConnection()
  13. {
  14. var connection = new SqlConnection(_connectionString);
  15. connection.Open();
  16. return connection;
  17. }
  18. public static SqlSugarClient GetInstance()
  19. {
  20. var db = new SqlSugarClient(new ConnectionConfig()
  21. {
  22. ConnectionString = _connectionString,
  23. DbType = SqlSugar.DbType.SqlServer,
  24. IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
  25. //InitKey默认SystemTable
  26. });
  27. db.Aop.OnLogExecuting = (sql, pars) =>
  28. {
  29. string result = sql + "\r\n";// + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
  30. LogHelper.Sys_Log.WriteLog(result);
  31. };
  32. return db;
  33. }
  34. #region 分页适用sqlserver数据库
  35. //public static PageData GetPageData<T>(List<string> WhereStringList, string orderText, int PageIndex, int PageSize)
  36. //{
  37. // var sb = new StringBuilder();
  38. // string tableName = typeof(T).Name;
  39. // sb.Append(string.Format("select * from {0} where 1=1 ", tableName));
  40. // if (WhereStringList != null && WhereStringList.Count > 0)
  41. // {
  42. // sb.Append(" and ");
  43. // sb.Append(WhereStringList[0]);
  44. // }
  45. // return QueryPageSql(sb.ToString(), orderText, PageIndex, PageSize);
  46. //}
  47. //public static PageData GetPageData(string _SQLText, string orderText, List<string> WhereStringList, int PageIndex, int PageSize)
  48. //{
  49. // var sb = new StringBuilder();
  50. // sb.Append(_SQLText);
  51. // if (WhereStringList != null && WhereStringList.Count > 0)
  52. // {
  53. // sb.Append(" and ");
  54. // sb.Append(WhereStringList[0]);
  55. // }
  56. // return QueryPageSql(sb.ToString(), orderText, PageIndex, PageSize);
  57. //}
  58. //private static PageData QueryPageSql(string SQLText, string OrderText, int PageIndex, int PageSize)
  59. //{
  60. // if (PageSize <= 0) PageSize = 2000;
  61. // int startRecord = (PageIndex - 1) * PageSize + 1;
  62. // int endRecord = startRecord + PageSize - 1;
  63. // string sql = string.Format("select * from (select ROW_NUMBER() Over (Order By {0}) as RN,a.* from ({1}) a) b where b.RN between {2} and {3}", OrderText, SQLText, startRecord, endRecord);
  64. // string CountStr = string.Format("Select Count(1) From ({0}) t", SQLText);
  65. // SqlSugarClient db = null;
  66. // try
  67. // {
  68. // db = GetInstance();
  69. // //WriteDebugFormat("查询sql:" + sql, "");
  70. // DataTable dt = db.Ado.GetDataTable(sql);
  71. // //WriteInfoFormat(dt.Rows[0]["Task_AddDateTime"].ToString(),string.Empty);
  72. // int count = Convert.ToInt32(db.Ado.GetScalar(CountStr));
  73. // int PageCount = count / PageSize;
  74. // if (count % PageSize > 0)
  75. // PageCount += 1;
  76. // if (dt != null && dt.Rows.Count > 0)
  77. // {
  78. // PageData PageDataItem = new PageData();
  79. // PageDataItem.PageIndex = PageIndex;
  80. // PageDataItem.PageSize = PageSize;
  81. // PageDataItem.PageCount = PageCount;
  82. // PageDataItem.RowsCount = count;
  83. // dt.TableName = "table";
  84. // PageDataItem.Dt = dt;
  85. // return PageDataItem;
  86. // }
  87. // }
  88. // catch (Exception ex)
  89. // {
  90. // MessageUtil.ShowError(ex.ToString());
  91. // }
  92. // finally
  93. // {
  94. // if (db != null) ((IDisposable)db).Dispose();
  95. // }
  96. // return null;
  97. //}
  98. #endregion;
  99. }
  100. public class CommHelper
  101. {
  102. #region 内存回收
  103. [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
  104. public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
  105. /// <summary>
  106. /// 释放内存
  107. /// </summary>
  108. public static void ClearMemory()
  109. {
  110. GC.Collect();
  111. GC.WaitForPendingFinalizers();
  112. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  113. {
  114. SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
  115. }
  116. }
  117. #endregion
  118. }
  119. //public class PageData
  120. //{
  121. // int _PageIndex = 0;
  122. // /// <summary>
  123. // /// 页码
  124. // /// </summary>
  125. // public int PageIndex
  126. // {
  127. // get
  128. // {
  129. // return _PageIndex;
  130. // }
  131. // set
  132. // {
  133. // _PageIndex = value;
  134. // }
  135. // }
  136. // int _PageSize = 0;
  137. // /// <summary>
  138. // /// 分页尺寸
  139. // /// </summary>
  140. // public int PageSize
  141. // {
  142. // get
  143. // {
  144. // return _PageSize;
  145. // }
  146. // set
  147. // {
  148. // _PageSize = value;
  149. // }
  150. // }
  151. // int _PageCount = 0;
  152. // /// <summary>
  153. // /// 总页数
  154. // /// </summary>
  155. // public int PageCount
  156. // {
  157. // get
  158. // {
  159. // return _PageCount;
  160. // }
  161. // set
  162. // {
  163. // _PageCount = value;
  164. // }
  165. // }
  166. // int _RowsCount = 0;
  167. // /// <summary>
  168. // /// 总记录数
  169. // /// </summary>
  170. // public int RowsCount
  171. // {
  172. // get
  173. // {
  174. // return _RowsCount;
  175. // }
  176. // set
  177. // {
  178. // _RowsCount = value;
  179. // }
  180. // }
  181. // string _DisplayText = "";
  182. // /// <summary>
  183. // /// 其他汇总数据文本
  184. // /// </summary>
  185. // public string DisplayText
  186. // {
  187. // get
  188. // {
  189. // return _DisplayText;
  190. // }
  191. // set
  192. // {
  193. // _DisplayText = value;
  194. // }
  195. // }
  196. // DataTable _Dt = null;
  197. // /// <summary>
  198. // /// 分页数据
  199. // /// </summary>
  200. // public DataTable Dt
  201. // {
  202. // get
  203. // {
  204. // return _Dt;
  205. // }
  206. // set
  207. // {
  208. // _Dt = value;
  209. // }
  210. // }
  211. //}
  212. }