/// <summary> /// 封装了对应的SqlMap的方法,异步分批执行查询并返回数据库表 /// </summary> /// <param name="statementName">语句名</param> /// <param name="parameterObject">参数名</param> /// <param name="skipResults"></param> /// <param name="maxResults"></param> /// <param name="postProcessTempDataHandler"></param> /// <param name="tempDataArrivalHandler"></param> /// <returns></returns> protected void AsynExecuteQueryForDataTable(string statementName, object parameterObject, int skipResults, int maxResults, PostProcessTempDataHandler postProcessTempDataHandler, TempDataArrivalHandler tempDataArrivalHandler) { ISqlMapper sqlMap = GetLocalSqlMap(); SafeDataReader dr = null; try { if (needDirtyRead && ContainsDirtyReadStatement()) { SetDirtyRead(); } dr = ExecuteDataReader(statementName, parameterObject); //using (SafeDataReader dr = ExecuteDataReader(statementName, parameterObject)) { //输出表 DataTable resultTable = null; int[,] indexMapping = null; // skip results for (int i = 0; i < skipResults; i++) { if (!dr.Read()) { break; } } int resultsFetched = 0; while ((maxResults == NO_MAXIMUM_RESULTS || resultsFetched < maxResults) && dr.Read()) { //while (dr.Read()) if (resultTable == null) { resultTable = CreateSchemaTable(dr, out indexMapping); } #region 填充数据 DataRow dataRow = (resultTable).NewRow(); int count = dr.FieldCount; for (int i = 0; i < count; i++) { if (indexMapping[1, i] != -1) { dataRow[indexMapping[1, i]] = dr[indexMapping[0, i]] ?? DBNull.Value; } } (resultTable).Rows.Add(dataRow); #endregion //临时数据送出 if (resultTable.Rows.Count > TMPRESULT_SIZE) { //数据后处理 if (postProcessTempDataHandler != null) { postProcessTempDataHandler(resultTable); } tempDataArrivalHandler(resultTable); resultTable = CreateSchemaTable(dr, out indexMapping); } resultsFetched++; } if (resultTable == null) { DataTable dataColumn = dr.GetSchemaTable(); if (dataColumn != null) { resultTable = new DataTable("statementName"); for (int i = 0; i < dataColumn.Rows.Count; i++) { resultTable.Columns.Add(dataColumn.Rows[i]["ColumnName"].ToString(), (Type)dataColumn.Rows[i]["DataType"]); } } } //数据后处理 if (postProcessTempDataHandler != null) { postProcessTempDataHandler(resultTable); } //最后数据送出 tempDataArrivalHandler(resultTable); //FireTempDataArrival(resultTable); } } catch (Exception e) { Trace.Write(e.Message + "\n" + e.StackTrace); throw new IBatisNetException( "Error executing query '" + statementName + "' for object. Cause: " + e.Message, e); } finally { if (dr != null) { dr.Dispose(); } } }
/// <summary> /// 封装了对应的SqlMap的方法,异步分批执行查询并返回数据库表 /// </summary> /// <param name="statementName">语句名</param> /// <param name="parameterObject">参数名</param> /// <param name="postProcessTempDataHandler"></param> /// <param name="tempDataArrivalHandler"></param> /// <returns></returns> protected void AsynExecuteQueryForDataTable(string statementName, object parameterObject, PostProcessTempDataHandler postProcessTempDataHandler, TempDataArrivalHandler tempDataArrivalHandler) { //强制最大值返回(为了兼容以前的代码,后续要改) if (ForceMaxResults) { AsynExecuteQueryForDataTable(statementName, parameterObject, -1, MAXRESULTS, postProcessTempDataHandler, tempDataArrivalHandler); } else //正常执行 { ISqlMapper sqlMap = GetLocalSqlMap(); SafeDataReader dr = null; try { if (needDirtyRead && ContainsDirtyReadStatement()) { SetDirtyRead(); } dr = ExecuteDataReader(statementName, parameterObject); //using (SafeDataReader dr = ExecuteDataReader(statementName, parameterObject)) { //输出表 DataTable resultTable = null; int[,] indexMapping = null; while (dr.Read()) { if (resultTable == null) { resultTable = CreateSchemaTable(dr, out indexMapping); } #region 填充数据 DataRow dataRow = resultTable.NewRow(); int count = dr.FieldCount; for (int i = 0; i < count; i++) { if (indexMapping[1, i] != -1) { dataRow[indexMapping[1, i]] = dr[indexMapping[0, i]] ?? DBNull.Value; } } resultTable.Rows.Add(dataRow); #endregion //临时数据送出 if (resultTable.Rows.Count > TMPRESULT_SIZE) { //数据后处理 if (postProcessTempDataHandler != null) { postProcessTempDataHandler(resultTable); } tempDataArrivalHandler(resultTable); resultTable = CreateSchemaTable(dr, out indexMapping); } } //数据后处理 if (postProcessTempDataHandler != null) { postProcessTempDataHandler(resultTable); } //最后数据送出 tempDataArrivalHandler(resultTable); //FireTempDataArrival(resultTable); } } catch (Exception e) { Trace.Write(e.Message + "\n" + e.StackTrace); throw new IBatisNetException( "Error executing query '" + statementName + "' for object. Cause: " + e.Message, e); } finally { if (dr != null) { dr.Dispose(); } } } }