private void InternalFind(TInput criteria, int maxResults, ProcedureQueryCallback <TOutput> callback) { SqlDataReader myReader = null; SqlCommand command = null; try { command = new SqlCommand(_procedureName, Context.Connection) { CommandType = CommandType.StoredProcedure, CommandTimeout = SqlServerSettings.Default.CommandTimeout }; UpdateContext update = Context as UpdateContext; if (update != null) { command.Transaction = update.Transaction; } // Set parameters SetParameters(command, criteria); if (Platform.IsLogLevelEnabled(LogLevel.Debug)) { Platform.Log(LogLevel.Debug, "Executing stored procedure: {0}", _procedureName); } myReader = command.ExecuteReader(); if (myReader == null) { Platform.Log(LogLevel.Error, "Unable to execute stored procedure '{0}'", _procedureName); command.Dispose(); return; } else { if (myReader.HasRows) { int resultCount = 0; Dictionary <string, PropertyInfo> propMap = EntityMapDictionary.GetEntityMap(typeof(TOutput)); while (myReader.Read()) { TOutput row = new TOutput(); PopulateEntity(myReader, row, propMap); callback(row); resultCount++; if (maxResults > 0 && resultCount >= maxResults) { break; } } myReader.Close(); myReader = null; } // Note: The retrieving of output parameters must occur after // the reader has been closed. GetOutputParameters(command, criteria); } } catch (Exception e) { Platform.Log(LogLevel.Error, e, "Unexpected exception when calling stored procedure: {0}", _procedureName); throw new PersistenceException(String.Format("Unexpected problem with stored procedure: {0}: {1}", _procedureName, e.Message), e); } finally { // Cleanup the reader/command, or else we won't be able to do anything with the // connection the next time here. if (myReader != null) { myReader.Close(); myReader.Dispose(); } if (command != null) { command.Dispose(); } } }
public void Find(TInput criteria, ProcedureQueryCallback <TOutput> callback) { InternalFind(criteria, -1, callback); }