/// <summary> /// 更新資料集 /// </summary> /// <param name="rowdata"></param> /// <returns></returns> public bool Update(List <T> rowdata) { UpdateCmd <T> cmd = new UpdateCmd <T>(dbEntity); IDbTransaction trans = globalTrans ?? cmd.Connection.BeginTransaction(); try { string str = ""; foreach (T row in rowdata) { str += cmd.GetUpdateCmd(row); } if (string.IsNullOrEmpty(str)) { return(false); } try { CommandDefinition cd = new CommandDefinition(commandText: str.ToString(), transaction: trans, commandTimeout: _cmdTimeout); cmd.Connection.Execute(cd); if (globalTrans == null) { trans.Commit(); } return(true); } catch (Exception err) { ErrLog.ExceptionLog(err, $"update rows to table {rowdata.GetType().Name} occur error."); if (globalTrans == null) { trans.Rollback(); } throw err; } finally { if (globalTrans == null) { trans.Dispose(); cmd.Connection.Close(); } } } catch (Exception err) { ErrLog.ExceptionLog(err, $"update commands occur error."); throw err; } }
/// <summary> /// 更新資料列 /// </summary> /// <param name="rowdata"></param> /// <returns></returns> public int Update(T rowdata) { UpdateCmd <T> cmd = new UpdateCmd <T>(dbEntity); try { var str = cmd.GetUpdateCmd(rowdata); if (string.IsNullOrEmpty(str)) { return(-1); } try { CommandDefinition cd = new CommandDefinition(commandText: str.ToString(), transaction: globalTrans, commandTimeout: _cmdTimeout); var row = cmd.Connection.Execute(cd); return(row); } catch (Exception err) { ErrLog.ExceptionLog(err, $"update table {rowdata.GetType().Name} occur error."); throw err; } finally { if (globalTrans == null) { cmd.Connection.Close(); } } } catch (Exception err) { ErrLog.ExceptionLog(err, $"update command occur error."); throw err; } }