/// <summary> /// 根据主键进行删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Delete(object id) { try { T t = Get(id); if (t != null) { return(Delete(t)); } else { return(false); } } catch (Exception ex) { LogHelper.Error(ex); WriteDataBase.CloseSharedConnection(); return(false); } finally { WriteDataBase.CloseSharedConnection(); } }
/// <summary> /// 更新或保存记录 /// </summary> /// <param name="t"></param> /// <returns></returns> public bool SaveOrUpdate(T t) { try { Type type = typeof(T); object objPrimary = type.GetProperty(PrimaryKey).GetValue(t, null); if (objPrimary == null) { return(Save(t)); } else { return(Update(t)); } } catch (Exception ex) { LogHelper.Error(ex); WriteDataBase.CloseSharedConnection(); return(false); } finally { WriteDataBase.CloseSharedConnection(); } }
/// <summary> /// 是否存在记录 /// </summary> /// <param name="sqlwhere">条件</param> /// <param name="files">查询字段</param> /// <param name="orderby">排序</param> /// <returns>bool</returns> public virtual T GetModel(string sqlwhere, string files, string orderby) { if (string.IsNullOrEmpty(sqlwhere)) { sqlwhere = "1=1"; } if (string.IsNullOrEmpty(files)) { files = "*"; } if (string.IsNullOrEmpty(orderby)) { orderby = "id desc"; } try { T t = WriteDataBase.SingleOrDefault <T>(Sql.Builder.Select(files).From(TableName).Where(sqlwhere).OrderBy(orderby)); return(t); } catch (Exception ex) { LogHelper.Error(ex); WriteDataBase.CloseSharedConnection(); return(null); } finally { WriteDataBase.CloseSharedConnection(); } }
/// <summary> /// 返回受影响的行数 /// </summary> /// <param name="sql">存储过程名称或则纯sql语句</param> /// <param name="cmdType">CommandType 【SQL脚本Text】 【简单存储过程StoredProcedure】</param> /// <param name="pms"></param> /// <returns></returns> public int ExecuteNoquery(string sql, CommandType cmdType, params SqlParameter[] pms) { try { WriteDataBase.OpenSharedConnection(); int flentCount = 0; try { using (var cmd = WriteDataBase.CreateCommand(WriteDataBase._sharedConnection, sql, true, pms)) { cmd.CommandType = cmdType; if (pms != null && pms.Count() > 0) { cmd.Parameters.Clear(); foreach (SqlParameter item in pms) { cmd.Parameters.Add(item); } } flentCount = cmd.ExecuteNonQuery(); return(flentCount); } } catch (Exception ex) { //todu.....异常日志,关闭连接 log.Info("ExecuteNoquery:" + ex); } finally { WriteDataBase.CloseSharedConnection(); } return(flentCount); } catch (Exception ex) { log.Info("ExecuteNoquery:" + ex); } finally { //todu.....异常日志,关闭连接 WriteDataBase.CloseSharedConnection(); } return(0); }
/// <summary> /// 获取一个集合 /// </summary> /// <param name="sqlwhere">条件</param> /// <returns>bool</returns> public List <T> GetModelList(string sql, params object[] args) { try { List <T> t = WriteDataBase.Query <T>(sql, args).ToList(); return(t); } catch (Exception ex) { LogHelper.Error(ex); WriteDataBase.CloseSharedConnection(); return(null); } finally { WriteDataBase.CloseSharedConnection(); } }
/// <summary> /// 根据实体进行删除 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual bool Delete(T t) { bool isSuccess = true; try { WriteDataBase.BeginTransaction(); isSuccess = WriteDataBase.Delete(TableName, "id", t) > 0; WriteDataBase.CompleteTransaction(); } catch (Exception ex) { LogHelper.Error(ex); WriteDataBase.CloseSharedConnection(); isSuccess = false; } finally { WriteDataBase.CloseSharedConnection(); } return(isSuccess); }
/// <summary> /// 根据实体进行更新操作 /// </summary> /// <param name="t">数据实体</param> /// <param name="primaryKey">库表主键</param> /// <returns></returns> public bool Update(T t, string primaryKey = "id") { bool isSuccess = true; try { WriteDataBase.BeginTransaction(); isSuccess = WriteDataBase.Update(TableName, primaryKey, t) > 0; WriteDataBase.CompleteTransaction(); } catch (Exception ex) { LogHelper.Error(ex); isSuccess = false; WriteDataBase.CloseSharedConnection(); } finally { WriteDataBase.CloseSharedConnection(); } return(isSuccess); }
/// <summary> /// 获取一个集合 /// </summary> /// <param name="sqlwhere">条件</param> /// <param name="files">查询字段</param> /// <param name="orderby">排序</param> /// <returns>bool</returns> public List <T> GetModelList(int top, string sqlwhere, string files, string orderby) { if (string.IsNullOrEmpty(sqlwhere)) { sqlwhere = "1=1"; } if (string.IsNullOrEmpty(files)) { files = "*"; } if (string.IsNullOrEmpty(orderby)) { orderby = "id desc"; } try { List <T> t = null; if (top != 0) { t = WriteDataBase.Query <T>(Sql.Builder.Select(string.Format("top {0} {1}", top, files)).From(TableName).Where(sqlwhere).OrderBy(orderby)).ToList(); } else { t = WriteDataBase.Query <T>(Sql.Builder.Select(files).From(TableName).Where(sqlwhere).OrderBy(orderby)).ToList(); } return(t); } catch (Exception ex) { LogHelper.Error(ex); WriteDataBase.CloseSharedConnection(); return(null); } finally { WriteDataBase.CloseSharedConnection(); } }
/// <summary> /// 根据实体进行软删除 /// </summary> /// <param name="t"></param> /// <returns></returns> public bool DeleteStatus(T t) { bool isSuccess = true; try { WriteDataBase.BeginTransaction(); isSuccess = WriteDataBase.DeleteStatus(t) > 0; WriteDataBase.CompleteTransaction(); } catch (Exception ex) { log.Info("Delete(T):" + ex); isSuccess = false; WriteDataBase.AbortTransaction(); WriteDataBase.CloseSharedConnection(); } finally { WriteDataBase.CloseSharedConnection(); } return(isSuccess); }
/// <summary> /// 保存该实体 /// </summary> /// <param name="t"></param> /// <returns></returns> public bool Save(T t) { bool isSuccess = true; try { WriteDataBase.BeginTransaction(); object obj = WriteDataBase.Insert(t); WriteDataBase.CompleteTransaction(); } catch (Exception ex) { log.Info("Save:" + ex); isSuccess = false; WriteDataBase.AbortTransaction(); WriteDataBase.CloseSharedConnection(); } finally { WriteDataBase.CloseSharedConnection(); } return(isSuccess); }
/// <summary> /// 保存实体 返回整形ID /// </summary> /// <param name="t">数据实体</param> /// <param name="primaryKey">库表主键</param> /// <returns></returns> public int Add(T t, string primaryKey = "id") { int isSuccess = 0; try { WriteDataBase.BeginTransaction(); object obj = WriteDataBase.Insert(TableName, primaryKey, t); isSuccess = Convert.ToInt32(obj); WriteDataBase.CompleteTransaction(); } catch (Exception ex) { LogHelper.Error(ex); isSuccess = 0; WriteDataBase.CloseSharedConnection(); LogHelper.Error(ex); } finally { WriteDataBase.CloseSharedConnection(); } return(isSuccess); }
/// <summary> /// 是否存在记录 /// </summary> /// <param name="id">主键ID</param> /// <returns>bool</returns> public virtual T GetModel(int id) { string sqlwhere = string.Empty; if (id > 0) { sqlwhere = "id=" + id; } try { T t = WriteDataBase.SingleOrDefault <T>(Sql.Builder.From(TableName).Where(sqlwhere)); return(t); } catch (Exception ex) { LogHelper.Error(ex); WriteDataBase.CloseSharedConnection(); return(null); } finally { WriteDataBase.CloseSharedConnection(); } }