using SqlSugar; using System; using System.Configuration; using System.Data.SqlClient; using System.Runtime.InteropServices; namespace WCS_Client { public class SugarClient { internal static string _connectionString = ConfigurationManager.ConnectionStrings["OracleDatabase"].ConnectionString; //创建sqlserver连接对象 public static SqlConnection GetConnection() { var connection = new SqlConnection(_connectionString); connection.Open(); return connection; } public static SqlSugarClient GetInstance() { var db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = _connectionString, DbType = SqlSugar.DbType.SqlServer, IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了 //InitKey默认SystemTable }); db.Aop.OnLogExecuting = (sql, pars) => { string result = sql + "\r\n";// + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)); LogHelper.Sys_Log.WriteLog(result); }; return db; } #region 分页适用sqlserver数据库 //public static PageData GetPageData(List WhereStringList, string orderText, int PageIndex, int PageSize) //{ // var sb = new StringBuilder(); // string tableName = typeof(T).Name; // sb.Append(string.Format("select * from {0} where 1=1 ", tableName)); // if (WhereStringList != null && WhereStringList.Count > 0) // { // sb.Append(" and "); // sb.Append(WhereStringList[0]); // } // return QueryPageSql(sb.ToString(), orderText, PageIndex, PageSize); //} //public static PageData GetPageData(string _SQLText, string orderText, List WhereStringList, int PageIndex, int PageSize) //{ // var sb = new StringBuilder(); // sb.Append(_SQLText); // if (WhereStringList != null && WhereStringList.Count > 0) // { // sb.Append(" and "); // sb.Append(WhereStringList[0]); // } // return QueryPageSql(sb.ToString(), orderText, PageIndex, PageSize); //} //private static PageData QueryPageSql(string SQLText, string OrderText, int PageIndex, int PageSize) //{ // if (PageSize <= 0) PageSize = 2000; // int startRecord = (PageIndex - 1) * PageSize + 1; // int endRecord = startRecord + PageSize - 1; // 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); // string CountStr = string.Format("Select Count(1) From ({0}) t", SQLText); // SqlSugarClient db = null; // try // { // db = GetInstance(); // //WriteDebugFormat("查询sql:" + sql, ""); // DataTable dt = db.Ado.GetDataTable(sql); // //WriteInfoFormat(dt.Rows[0]["Task_AddDateTime"].ToString(),string.Empty); // int count = Convert.ToInt32(db.Ado.GetScalar(CountStr)); // int PageCount = count / PageSize; // if (count % PageSize > 0) // PageCount += 1; // if (dt != null && dt.Rows.Count > 0) // { // PageData PageDataItem = new PageData(); // PageDataItem.PageIndex = PageIndex; // PageDataItem.PageSize = PageSize; // PageDataItem.PageCount = PageCount; // PageDataItem.RowsCount = count; // dt.TableName = "table"; // PageDataItem.Dt = dt; // return PageDataItem; // } // } // catch (Exception ex) // { // MessageUtil.ShowError(ex.ToString()); // } // finally // { // if (db != null) ((IDisposable)db).Dispose(); // } // return null; //} #endregion; } public class CommHelper { #region 内存回收 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")] public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize); /// /// 释放内存 /// public static void ClearMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } } #endregion } //public class PageData //{ // int _PageIndex = 0; // /// // /// 页码 // /// // public int PageIndex // { // get // { // return _PageIndex; // } // set // { // _PageIndex = value; // } // } // int _PageSize = 0; // /// // /// 分页尺寸 // /// // public int PageSize // { // get // { // return _PageSize; // } // set // { // _PageSize = value; // } // } // int _PageCount = 0; // /// // /// 总页数 // /// // public int PageCount // { // get // { // return _PageCount; // } // set // { // _PageCount = value; // } // } // int _RowsCount = 0; // /// // /// 总记录数 // /// // public int RowsCount // { // get // { // return _RowsCount; // } // set // { // _RowsCount = value; // } // } // string _DisplayText = ""; // /// // /// 其他汇总数据文本 // /// // public string DisplayText // { // get // { // return _DisplayText; // } // set // { // _DisplayText = value; // } // } // DataTable _Dt = null; // /// // /// 分页数据 // /// // public DataTable Dt // { // get // { // return _Dt; // } // set // { // _Dt = value; // } // } //} }