/// <summary> /// 刪除資料列 /// </summary> /// <param name="rowdata"></param> /// <returns></returns> public bool Delete(T rowdata) { DeleteCmd <T> cmd = new DeleteCmd <T>(dbEntity); string sql = cmd.GetDeleteCmd(rowdata); if (string.IsNullOrEmpty(sql)) { return(false); } try { CommandDefinition cd = new CommandDefinition(commandText: sql, commandTimeout: _cmdTimeout, transaction: globalTrans); cmd.Connection.Execute(sql); return(true); } catch (Exception err) { ErrLog.ExceptionLog(err, $"delete row from {rowdata.GetType().Name} occur error."); return(false); } finally { if (globalTrans == null) { cmd.Connection.Close(); } } }
/// <summary> /// 刪除資料集 /// </summary> /// <param name="rowdata"></param> /// <returns></returns> public bool Delete(List <T> rowdata) { DeleteCmd <T> cmd = new DeleteCmd <T>(dbEntity); IDbTransaction trans = globalTrans ?? cmd.Connection.BeginTransaction(); StringBuilder sql = new StringBuilder(); foreach (T row in rowdata) { sql.Append(cmd.GetDeleteCmd(row)); } if (string.IsNullOrEmpty(sql.ToString())) { return(false); } try { CommandDefinition cd = new CommandDefinition(commandText: sql.ToString(), transaction: trans, commandTimeout: _cmdTimeout); cmd.Connection.Execute(cd); if (globalTrans == null) { trans.Commit(); } return(true); } catch (Exception err) { ErrLog.ExceptionLog(err, $"delete rows from {rowdata.GetType().Name} occur error."); if (globalTrans == null) { trans.Rollback(); } return(false); } finally { if (globalTrans == null) { trans.Dispose(); cmd.Connection.Close(); } } }