/// <summary> /// 主体查询预备执行操作,这里会构造命令日志信息 /// </summary> /// <param name="db">当前查询连接对象</param> /// <param name="SQL">当前主体要执行的查询命令</param> /// <param name="commandType">命令类型</param> /// <param name="parameters">命令参数</param> /// <returns>总是返回成功</returns> public bool OnExecuting(CommonDB db, ref string SQL, CommandType commandType, IDataParameter[] parameters) { if (this.enable) { this.currDb = db; db.BeginTransaction(); //需要真实反映执行的语句顺序,CommandID的赋值推迟到执行后 //logEntity.CommandID = CommonUtil.NewSequenceGUID(); //logEntity.ExecuteTime = DateTime.Now; //使用 PrepairSQL 方法处理 //logEntity.CommandText = SQL; logEntity.CommandType = commandType; logEntity.LogFlag = 0; //logEntity.ParameterInfo = DbParameterSerialize.Serialize(parameters); logEntity.PrepairSQL(SQL, DbParameterSerialize.Serialize(parameters)); if (db.ContextObject != null) { if (db.ContextObject is OQL) { logEntity.CommandName = ((OQL)db.ContextObject).currEntity.GetTableName(); } else if (db.ContextObject is EntityBase) { logEntity.CommandName = ((EntityBase)db.ContextObject).GetTableName(); } else { logEntity.CommandName = ""; } } } return(true); }
/// <summary> /// 主体查询预备执行操作,这里会构造命令日志信息 /// </summary> /// <param name="db">当前查询连接对象</param> /// <param name="SQL">当前主体要执行的查询命令</param> /// <param name="commandType">命令类型</param> /// <param name="parameters">命令参数</param> /// <returns>总是返回成功</returns> public bool OnExecuting(CommonDB db, ref string SQL, CommandType commandType, IDataParameter[] parameters) { if (this.enable) { this.currDb = db; db.BeginTransaction(); logEntity.CommandID = CommonUtil.NewSequenceGUID(); logEntity.CommandText = SQL; logEntity.CommandType = commandType; logEntity.LogFlag = 0; logEntity.ParameterInfo = DbParameterSerialize.Serialize(parameters); logEntity.ExecuteTime = DateTime.Now; } return(true); }
/// <summary> /// 自动保存数据表中的数据到数据库 /// <remarks>更新时间:2011.11.16</remarks> /// </summary> /// <param name="dt"></param> /// <param name="insertSQL"></param> /// <param name="updateSQL"></param> /// <param name="DB"></param> /// <returns></returns> private static int SaveDataTable(DataTable dt, string insertSQL, string updateSQL, CommonDB DB) { //CommonDB DB = MyDB.GetDBHelper(); string ParaChar = GetDBParaChar(DB); insertSQL = insertSQL.Replace("@@", ParaChar); updateSQL = updateSQL.Replace("@@", ParaChar); int count = 0; DB.BeginTransaction(); try { foreach (DataRow dr in dt.Rows) { IDataParameter[] paras = new IDataParameter[dt.Columns.Count]; for (int i = 0; i < dt.Columns.Count; i++) { paras[i] = DB.GetParameter(ParaChar + dt.Columns[i].ColumnName, dr[i]); } //先更新,如果没有记录受影响再次尝试执行插入 int tempCount = DB.ExecuteNonQuery(updateSQL, CommandType.Text, paras); if (tempCount <= 0) tempCount = DB.ExecuteNonQuery(insertSQL, CommandType.Text, paras); count += tempCount; if (DB.ErrorMessage != "") throw new Exception(DB.ErrorMessage); } DB.Commit(); } catch (Exception ex) { DB.Rollback(); throw ex; } return count; }