private void Save(int objId, int tryCount) { if (this.LogId > 0) { throw new Exception("日志 " + this.LogId + " 已保存过,不能再次保存"); } DateTime dtmLodId; lock (st_LogIdLock) { dtmLodId = st_LogIdLock.GetLogId(); } string strTableName = Tables.Log.GetTableName(dtmLodId); Database.DbConnection dbConnection = this.Application.GetDbConnection(); if (dbConnection.TableIsExist(strTableName) == false) { dbConnection.CreateTable(typeof(Tables.Log), strTableName); } string strConfig = null; System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); System.Xml.XmlNode xnLogConfig = this.GetLogConfig(xmlDoc); if (xnLogConfig != null) { xmlDoc.AppendChild(xnLogConfig); strConfig = xmlDoc.OuterXml; } try { string strSql = dtmLodId.Ticks.ToString() + "," + Function.SqlStr(this.GetType().FullName, 250) + "," + objId + "," + Function.SqlStr(strConfig); strSql = "INSERT INTO " + strTableName + " (" + Tables.Log.LogId + "," + Tables.Log.LogType + "," + Tables.Log.ObjId + "," + Tables.Log.XMLConfig + ") VALUES (" + strSql + ")"; dbConnection.ExecuteNonQuery(strSql); this.m_LogId = dtmLodId.Ticks; } catch { if (tryCount > 3) { throw new Exception("保存日志 " + this.GetType().FullName + " 时发生错误,已重试3次。"); } else { this.Save(objId, tryCount + 1); } } finally { dbConnection.Close(); } }
private void Save(int tryCount) { if (this.LogId > 0) { throw new Exception("任务运行日志 " + this.LogId + " 已保存过,不能再次保存"); } DateTime dtmLodId = DateTime.Now; string strTableName = Tables.TaskLog.GetTableName(dtmLodId); Database.DbConnection dbConnection = this.Config.Application.GetDbConnection(); if (dbConnection.TableIsExist(strTableName) == false) { dbConnection.CreateTable(typeof(Tables.TaskLog), strTableName); } try { string strSql = Function.SqlStr(dtmLodId.Ticks.ToString()) + "," + this.Config.TaskConfigId.ToString() + "," + Function.BoolToByte(this.IsAuto) + "," + this.PlanTime.Ticks.ToString() + ",0,0," + Function.SqlStr(this.TaskName); strSql = "INSERT INTO " + strTableName + " (" + Tables.TaskLog.TaskLogId + "," + Tables.TaskLog.TaskConfigId + "," + Tables.TaskLog.IsAuto + "," + Tables.TaskLog.PlanTime + "," + Tables.TaskLog.RetryTimes + "," + Tables.TaskLog.LastProgress + "," + Tables.TaskLog.TaskName + ") VALUES (" + strSql + ")"; dbConnection.ExecuteNonQuery(strSql); this.LogId = dtmLodId.Ticks; } catch { if (tryCount > 3) { throw new Exception("保存任务运行日志 " + this.GetType().FullName + " 时发生错误,已重试3次。"); } else { this.Save(tryCount + 1); } } finally { dbConnection.Close(); } }
private int CountReadIndex = 0; //已读取的 Count 最后的索引位置。 /// <summary> /// 开始搜索指定页数的数据。 /// </summary> /// <param name="pageNum">搜索第几页的数据。当此参数为“0”时,则表示不进行分页,将所有符合条件的数据全部读取出。</param> /// <returns>搜索结果集合</returns> public TaskLogCollection Search(int pageNum) { this.CountReadIndex = 0; this.m_PageNum = pageNum; TaskLogCollection taskLogs = new TaskLogCollection(); IList <AC.Base.DateRange.MonthlyForTickRange> lstTickRanges = this.DateRange.GetMonthTickRanges(); int[] intCounts = new int[lstTickRanges.Count]; Database.DbConnection dbConn = this.Application.GetDbConnection(); try { if (this.PageSize > 0) { this.m_RecordsetCount = 0; for (int intIndex = 0; intIndex < lstTickRanges.Count; intIndex++) { AC.Base.DateRange.MonthlyForTickRange range = lstTickRanges[intIndex]; string strTableName = Tables.TaskLog.GetTableName(range.Date); if (dbConn.TableIsExist(strTableName)) { string strSql = "SELECT COUNT(*) FROM " + strTableName + this.GetSqlWhere(range); System.Data.IDataReader dr = dbConn.ExecuteReader(strSql); if (dr.Read()) { intCounts[intIndex] = Function.ToInt(dr[0]); this.m_RecordsetCount += intCounts[intIndex]; } } } this.m_PageCount = this.RecordsetCount / this.PageSize; if ((this.RecordsetCount % this.PageSize) > 0) { this.m_PageCount++; } if (this.PageNum < 1) { this.m_PageNum = 1; } if (this.PageNum > this.PageCount) { this.m_PageNum = this.PageCount; } } if (this.PageSize == 0 || this.PageCount == 1) { this.m_RecordsetCount = 0; if (this.m_OrderIsDesc) { for (int intCountIndex = lstTickRanges.Count - 1; intCountIndex >= 0; intCountIndex--) { AC.Base.DateRange.MonthlyForTickRange tickRange = lstTickRanges[intCountIndex]; string strTableName = Tables.TaskLog.GetTableName(tickRange.Date); if (dbConn.TableIsExist(strTableName)) { string strSql = "SELECT * FROM " + strTableName + this.GetSqlWhere(tickRange) + this.GetOrderColumn(this.m_OrderIsDesc); System.Data.IDataReader dr = dbConn.ExecuteReader(strSql); while (dr.Read()) { this.m_RecordsetCount++; taskLogs.Add(DrToTaskLog(dr)); } dr.Close(); } } } else { for (int intCountIndex = 0; intCountIndex < lstTickRanges.Count; intCountIndex++) { AC.Base.DateRange.MonthlyForTickRange tickRange = lstTickRanges[intCountIndex]; string strTableName = Tables.TaskLog.GetTableName(tickRange.Date); if (dbConn.TableIsExist(strTableName)) { string strSql = "SELECT * FROM " + strTableName + this.GetSqlWhere(tickRange) + this.GetOrderColumn(this.m_OrderIsDesc); System.Data.IDataReader dr = dbConn.ExecuteReader(strSql); while (dr.Read()) { this.m_RecordsetCount++; taskLogs.Add(DrToTaskLog(dr)); } dr.Close(); } } } if (this.m_RecordsetCount == 0) { this.m_PageCount = 0; this.m_PageNum = 0; } else { this.m_PageCount = 1; this.m_PageNum = 1; } } else { int intRsStartIndex = this.RecordsetStartIndex; int intRsEndIndex = this.RecordsetEndIndex; if (this.m_OrderIsDesc) { for (int intCountIndex = intCounts.Length - 1; intCountIndex >= 0; intCountIndex--) { this.Search(dbConn, taskLogs, lstTickRanges, intCounts, intCountIndex, intRsStartIndex, intRsEndIndex); } } else { for (int intCountIndex = 0; intCountIndex < intCounts.Length; intCountIndex++) { this.Search(dbConn, taskLogs, lstTickRanges, intCounts, intCountIndex, intRsStartIndex, intRsEndIndex); } } } for (int intIndex = 0; intIndex < lstTickRanges.Count; intIndex++) { string strLogIds = ""; AC.Base.DateRange.MonthlyForTickRange range = lstTickRanges[intIndex]; foreach (TaskLog log in taskLogs) { if (range.Contains(log.LogId)) { strLogIds += "," + log.LogId.ToString(); } } if (strLogIds.Length > 0) { strLogIds = strLogIds.Substring(1); string strTableName; strTableName = Tables.TaskRunLog.GetTableName(range.Date); if (dbConn.TableIsExist(strTableName)) { string strSql = "SELECT * FROM " + strTableName + " WHERE " + Tables.TaskRunLog.TaskLogId + " IN (" + strLogIds + ")"; System.Data.IDataReader dr = dbConn.ExecuteReader(strSql); while (dr.Read()) { long lngLogId = Function.ToLong(dr[Tables.TaskRunLog.TaskLogId]); foreach (TaskLog log in taskLogs) { if (log.LogId == lngLogId) { log.AddTask(dr); break; } } } } } } } catch (Exception ex) { throw ex; } finally { dbConn.Close(); } return(taskLogs); }