private int GetCount(QuerySection <T> query) { string countString = query.CountString; if (query.unionQuery) { countString = "select sum(row_count) as total_row_count from (" + countString + ") tmp_table"; } string cacheKey = GetCacheKey(countString, this.Parameters); object obj = GetCache <T>("Count", cacheKey); if (obj != null) { return(DataUtils.ConvertValue <int>(obj)); } //添加参数到Command中 queryCommand = dbProvider.CreateSqlCommand(countString, query.Parameters); object value = dbProvider.ExecuteScalar(queryCommand, dbTran); int ret = DataUtils.ConvertValue <int>(value); SetCache <T>("Count", cacheKey, ret); return(ret); }
private int GetCount(QuerySection <T> query) { string countString = query.CountString; string cacheKey = GetCacheKey(countString, this.Parameters); object obj = GetCache <T>("Count", cacheKey); if (obj != null) { return(CoreHelper.ConvertValue <int>(obj)); } //添加参数到Command中 queryCommand = dbProvider.CreateSqlCommand(countString, query.Parameters); object value = dbProvider.ExecuteScalar(queryCommand, dbTran); int ret = CoreHelper.ConvertValue <int>(value); SetCache <T>("Count", cacheKey, ret); return(ret); }
internal SqlSection(string sql, DbProvider dbProvider, DbTrans dbTran) { this.dbProvider = dbProvider; this.dbTran = dbTran; this.dbCommand = dbProvider.CreateSqlCommand(sql); }
/// <summary> /// 执行批处理操作 /// </summary> /// <param name="errors">输出的错误</param> /// <returns></returns> public int Process(out IList <MySoftException> errors) { //实例化errors errors = new List <MySoftException>(); int rowCount = 0; if (commandList.Count == 0) { //如果命令列表为空,则直接返回 return(rowCount); } //Access不能进行多任务处理 if (!dbProvider.SupportBatch) { foreach (DbCommand cmd in commandList) { try { //执行成功,则马上退出 rowCount += dbProvider.ExecuteNonQuery(cmd, dbTrans); } catch (DbException ex) { errors.Add(new MySoftException(ex.Message, ex)); } //执行一次休眠一下 Thread.Sleep(10); } } else { int size = Convert.ToInt32(Math.Ceiling(commandList.Count * 1.0 / batchSize)); for (int index = 0; index < size; index++) { DbCommand mergeCommand = dbProvider.CreateSqlCommand("init"); List <DbCommand> cmdList = new List <DbCommand>(); int getSize = batchSize; if ((index + 1) * batchSize > commandList.Count) { getSize = commandList.Count - index * batchSize; } cmdList.AddRange(commandList.GetRange(index * batchSize, getSize)); StringBuilder sb = new StringBuilder(); int pIndex = 0; foreach (DbCommand cmd in cmdList) { string cmdText = cmd.CommandText; foreach (DbParameter p in cmd.Parameters) { DbParameter newp = (DbParameter)((ICloneable)p).Clone(); mergeCommand.Parameters.Add(newp); } sb.Append(cmdText); sb.Append(";\n"); pIndex++; } mergeCommand.CommandText = sb.ToString(); try { //执行成功,则马上退出 rowCount += dbProvider.ExecuteNonQuery(mergeCommand, dbTrans); } catch (DbException ex) { errors.Add(new MySoftException(ex.Message, ex)); } //执行一次休眠一下 Thread.Sleep(10); } } //结束处理,清除命令列表 commandList.Clear(); return(rowCount); }