Пример #1
0
        } //end of Retrieve()

        // retrieves a list of objects
        public object RetrieveAll(Type type)
        {
            List <EventProps> list   = new List <EventProps>();
            DBDataReader      reader = null;
            EventProps        props;

            try
            {
                reader = RunProcedure("usp_EventSelectAll");
                if (!reader.IsClosed)
                {
                    while (reader.Read())
                    {
                        props = new EventProps();
                        props.SetState(reader);
                        list.Add(props);
                    }
                }
                return(list);
            }
            catch (Exception e)
            {
                // log this exception
                throw;
            }
            finally
            {
                if (!reader.IsClosed)
                {
                    reader.Close();
                }
            }
        }
        // *** I deleted IndexOf and NextID.
        // I'll use the database to find the record and
        // determine the value of a new EventId

        // *** The body of all of these methods are different
        // They use ADO.NET objects and call methods in the SQL base class
        #region IReadDB Members
        /// <summary>
        /// </summary>
        ///
        public IBaseProps Retrieve(Object key)
        {
            DBDataReader data    = null;
            EventProps   props   = new EventProps();
            DBCommand    command = new DBCommand();

            command.CommandText = "usp_EventSelect";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@EventID", SqlDbType.Int);
            command.Parameters["@EventID"].Value = (Int32)key;

            try
            {
                data = RunProcedure(command);
                // *** I added this version of SetState to the props class
                if (!data.IsClosed)
                {
                    if (data.Read())
                    {
                        props.SetState(data);
                    }
                    else
                    {
                        throw new Exception("Record does not exist in the database.");
                    }
                }
                return(props);
            }
            catch (Exception e)
            {
                // log this exception
                throw;
            }
            finally
            {
                if (data != null)
                {
                    if (!data.IsClosed)
                    {
                        data.Close();
                    }
                }
            }
        } //end of Retrieve()