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(); } }
/// <summary> /// 保存设备日温度曲线数据段(非完整的曲线数据,曲线中的一个点或连续的几个点),如果数据点数大于96点,则每次保存时一次传入15分钟周期的全部数据,将获得最佳性能。(每月产生一张名为 TemperatureF 或 TemperatureS 的数据表) /// </summary> /// <param name="date">数据段中数据的起始日期及时间。使用其中的年、月、日、时、分、秒。。</param> /// <param name="curvePoint">该数据的点数类型。</param> /// <param name="values">日温度曲线数据段值,该数组仅存放曲线中的一部分数据。</param> /// <param name="save96Point">当 curvePoint 大于 96 点时是否另外计算一份96点的数据进行保存。当 curvePoint 小于等于 96 点时该参数无任何作用。</param> public void Save(DateTime date, CurvePointOptions curvePoint, decimal?[] values, CurvePartDataSave96PointOptions save96Point) { Database.DbConnection dbConn = this.Device.Application.GetDbConnection(); try { Save(dbConn, this.Device.DeviceId, date, curvePoint, values, save96Point); } catch (Exception ex) { throw ex; } finally { dbConn.Close(); } }
/// <summary> /// 保存设备日温度曲线数据,如果数据点数大于96点,则另外再计算保存一份96点数据。(每月产生一张名为 TemperatureF 或 TemperatureS 的数据表) /// </summary> /// <param name="date">数据日期。只使用其中的年、月、日。</param> /// <param name="values">日温度曲线数据值,该数组长度必须是 CurvePointOptions 中指定的长度之一。</param> public void Save(DateTime date, decimal?[] values) { Database.DbConnection dbConn = this.Device.Application.GetDbConnection(); try { Save(dbConn, this.Device.DeviceId, date, values); } catch (Exception ex) { throw ex; } finally { dbConn.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); }