List <TOutput> IEnumBroker <TOutput> .Execute()
        {
            List <TOutput> list      = new List <TOutput>();
            TOutput        tempValue = new TOutput();

            SqlDataReader myReader = null;
            SqlCommand    command  = null;

            try
            {
                command = new SqlCommand(String.Format("SELECT * FROM {0}", tempValue.Name), Context.Connection)
                {
                    CommandType    = CommandType.Text,
                    CommandTimeout = SqlServerSettings.Default.CommandTimeout
                };

                myReader = command.ExecuteReader();
                if (myReader == null)
                {
                    Platform.Log(LogLevel.Error, "Unable to select contents of '{0}'", tempValue.Name);
                    command.Dispose();
                    return(list);
                }
                else
                {
                    if (myReader.HasRows)
                    {
                        Dictionary <string, PropertyInfo> propMap = EntityMapDictionary.GetEntityMap(typeof(TOutput));

                        while (myReader.Read())
                        {
                            TOutput row = new TOutput();

                            PopulateEntity(myReader, row, propMap);

                            list.Add(row);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Platform.Log(LogLevel.Error, e, "Unexpected exception when retrieving enumerated value: {0}", tempValue.Name);

                throw new PersistenceException(String.Format("Unexpected problem when retrieving enumerated value: {0}: {1}", tempValue.Name, 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();
                }
            }
            return(list);
        }
Example #2
0
        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();
                }
            }
        }