using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace MainForm.DbHelper { /// /// /// public class SqlHelper : PracticeContext { #region 查询 /// /// 查询实体 /// /// 实体 /// public List Queryable() where T : class, new() { return Db.Queryable().ToList(); } /// /// 根据表达式查询实体 /// /// 实体 /// Where条件 /// public List Queryable(Expression> expression) where T : class, new() { return Db.Queryable().Where(expression).ToList(); } /// /// 根据表达式查询第一条实体 /// /// 实体 /// Where条件 /// 排序字段 /// public T QueryableAscFirst(Expression> expression, string orderFileds = null) where T : class, new() { if (orderFileds == null) { return Db.Queryable().Where(expression).First(); } return Db.Queryable().Where(expression).OrderBy(orderFileds).First(); } /// /// SQL语句查询 /// /// 实体 /// SQL执行语句 /// public List SqlQueryable(string sql) where T : class, new() { return Db.SqlQueryable(sql).ToList(); } /// /// SQL语句执行 /// /// SQL执行语句 /// public bool SqlExecuteCommand(string sql) { if (string.IsNullOrEmpty(sql)) return true; try { int count = Db.Ado.ExecuteCommand(sql); return count > 0; } catch (Exception e) { return ExceptionCapture(e); } } #endregion #region 新增 /// /// 新增 /// /// 实体 /// 数据 /// 状态 public bool Insert(T insertObj) where T : class, new() { if (insertObj == null) return true; try { return Db.Insertable(insertObj).ExecuteCommandIdentityIntoEntity(); } catch (Exception e) { throw e; } } /// /// 批量新增 /// /// 实体 /// 数据 /// 状态 public bool Insert(List insertObjs) where T : class, new() { if (!insertObjs.Any()) return true; try { //分页操作 ,如果不支持db.Fastest分页插入也是可以提升一下性能的 Db.Utilities.PageEach(insertObjs, 100, pageList => { int result = Db.Insertable(pageList).ExecuteCommand(); if (result != pageList.Count) { throw new Exception($"批量新增失败,计划操作{pageList.Count}条,实际操作{result}条"); } }); return true; } catch (Exception e) { return ExceptionCapture(e); } } #endregion #region 修改 /// /// 根据主键修改 /// /// 实体 /// 数据 /// 状态 public bool Update(T UpdateObj) where T : class, new() { if (UpdateObj == null) return true; try { return Db.Updateable(UpdateObj).ExecuteCommandHasChange(); } catch (Exception e) { return ExceptionCapture(e); } } /// /// 根据主键修改实体指定列 /// /// /// 数据 /// 需要更新的实体列 /// public bool Update(T UpdateObj, Expression> columns) where T : class, new() { if (UpdateObj == null) return true; try { return Db.Updateable(UpdateObj).UpdateColumns(columns).ExecuteCommandHasChange(); } catch (Exception e) { return ExceptionCapture(e); } } /// /// 根据主键批量修改 /// /// 实体 /// 数据 /// 状态 public bool Update(List UpdateObjs) where T : class, new() { if (!UpdateObjs.Any()) return true; try { //分页操作 ,如果不支持db.Fastest分页插入也是可以提升一下性能的 Db.Utilities.PageEach(UpdateObjs, 100, pageList => { int result = Db.Updateable(pageList).ExecuteCommand(); if (result != pageList.Count) { throw new Exception($"批量修改失败,计划操作{pageList.Count}条,实际操作{result}条"); } }); return true; } catch (Exception e) { return ExceptionCapture(e); } } #endregion #region 删除 /// /// 根据主键删除 /// /// 实体 /// 主键类型 /// 主键 /// 状态 public bool Deleteable(PkType primaryKeyValue) where T : class, new() { try { return Db.Deleteable().In(primaryKeyValue).ExecuteCommandHasChange(); } catch (Exception e) { return ExceptionCapture(e); } } /// /// 根据表达式删除 /// /// 实体 /// Where条件 /// 状态 public bool Deleteable(Expression> expression) where T : class, new() { try { return Db.Deleteable().Where(expression).ExecuteCommandHasChange(); } catch (Exception e) { return ExceptionCapture(e); } } /// /// 根据主键批量删除 /// /// 实体 /// 数据 /// 状态 public bool Deleteable(List deleteObjs) where T : class, new() { if (!deleteObjs.Any()) return true; try { //分页操作 ,如果不支持db.Fastest分页插入也是可以提升一下性能的 Db.Utilities.PageEach(deleteObjs, 100, pageList => { int result = Db.Deleteable(pageList).ExecuteCommand(); if (result != pageList.Count) { throw new Exception($"批量删除失败,计划操作{pageList.Count}条,实际操作{result}条"); } }); return true; } catch (Exception e) { return ExceptionCapture(e); } } #endregion #region 事务 (private,后期扩展增删改方法直接把事务方法添加进去;或者根据需求public) /// /// 开始事务 /// private void BeginTran() { Db.Ado.BeginTran(); } /// /// 提交事务 /// private void CommitTran() { Db.Ado.CommitTran(); } /// /// 回滚事务 /// private void RollbackTran() { Db.Ado.RollbackTran(); } #endregion #region 异常捕捉 /// /// 异常捕捉 /// /// /// private bool ExceptionCapture(Exception ex) { Console.WriteLine("\n异常信息:\n{0}", ex.Message); return false; } #endregion } }