} // ForEachResult public static IEnumerable <SafeReader> ExecuteEnumerable( this DbCommand command, ConnectionWrapper cw, AConnection oConnection, Action oLogExecution = null ) { bool bAllesInOrdnung = false; try { cw.Open(); DbDataReader oReader = command.ExecuteReader(); if (oLogExecution != null) { oLogExecution(); } do { if (!oReader.HasRows) { continue; } while (oReader.Read()) { yield return(new SafeReader(oReader)); } } while (oReader.NextResult()); oReader.Close(); bAllesInOrdnung = true; } finally { if (oConnection != null) { oConnection.DisposeAfterOneUsage(bAllesInOrdnung, cw); } } // try } // ExecuteEnumerable
} // Run protected virtual object RunOnce( ConnectionWrapper oConnectionToUse, Func <DbDataReader, bool, ActionResult> oAction, ExecMode nMode, DbCommand command, LogVerbosityLevel nLogVerbosityLevel, string spName, string sArgsForLog, Guid guid ) { ConnectionWrapper oConnection = null; bool bAllesInOrdnung = true; bool bDropAfterUse = oConnectionToUse == null; try { oConnection = oConnectionToUse ?? TakeFromPool(); if (oConnection == null) { throw new NullReferenceException("There is no available connection to execute the query."); } command.Connection = oConnection.Connection; if (oConnection.Transaction != null) { command.Transaction = oConnection.Transaction; } string sPooledConID = oConnection.Pooled.Name; var sw = new Stopwatch(); sw.Start(); switch (nMode) { case ExecMode.Scalar: oConnection.Open(); object value = command.ExecuteScalar(); PublishRunningTime(sPooledConID, nLogVerbosityLevel, spName, sArgsForLog, guid, sw); return(value); case ExecMode.Reader: oConnection.Open(); var oReader = command.ExecuteReader(); long nPrevStopwatchValue = sw.ElapsedMilliseconds; if (nLogVerbosityLevel == LogVerbosityLevel.Verbose) { PublishRunningTime(sPooledConID, nLogVerbosityLevel, spName, sArgsForLog, guid, sw); } var dataTable = new DataTable(); dataTable.Load(oReader); string sMsg; switch (nLogVerbosityLevel) { case LogVerbosityLevel.Compact: sMsg = "completed and data loaded"; break; case LogVerbosityLevel.Verbose: sMsg = "data loaded"; break; default: throw new ArgumentOutOfRangeException(); } // switch PublishRunningTime(sPooledConID, nLogVerbosityLevel, spName, sArgsForLog, guid, sw, nPrevStopwatchValue, sMsg); oReader.Close(); return(dataTable); case ExecMode.NonQuery: oConnection.Open(); int nResult = command.ExecuteNonQuery(); string sResult = ((nResult == 0) || (nResult == -1)) ? "no" : nResult.ToString(CultureInfo.InvariantCulture); PublishRunningTime(sPooledConID, nLogVerbosityLevel, spName, sArgsForLog, guid, sw, sAuxMsg: string.Format("- {0} row{1} changed", sResult, nResult == 1 ? "" : "s")); return(nResult); case ExecMode.ForEachRow: oConnection.Open(); command.ForEachRow(oAction, () => PublishRunningTime(sPooledConID, nLogVerbosityLevel, spName, sArgsForLog, guid, sw)); return(null); case ExecMode.Enumerable: return(command.ExecuteEnumerable( oConnection, bDropAfterUse ? this : null, () => PublishRunningTime(sPooledConID, nLogVerbosityLevel, spName, sArgsForLog, guid, sw) )); default: throw new ArgumentOutOfRangeException("nMode"); } // switch } catch (Exception) { bAllesInOrdnung = false; throw; } finally { if (bDropAfterUse && (nMode != ExecMode.Enumerable)) { DisposeAfterOneUsage(bAllesInOrdnung, oConnection); } } // try } // RunOnce