示例#1
0
        /// <summary>
        /// Get all the entries in the ItemPricing table, with the specified ItemId
        /// </summary>
        public static IEnumerable <ItemPricing> GetAll(int itemId, bool nowOnly, bool enableRequired)
        {
            string cmdText = "SELECT * FROM ItemPricing WHERE (ItemPricingItemId=" + itemId + ")";

            if (nowOnly)
            {
                cmdText += " AND (ItemPricingDayOfWeek=@ItemPricingDayOfWeek)" +
                           " AND ((ItemPricingStartDate IS NULL) OR (ItemPricingStartDate >= @CurrentTime))" +
                           " AND ((ItemPricingEndDate IS NULL) OR (ItemPricingEndDate <= @CurrentTime))";
            }
            if (enableRequired)
            {
                cmdText += " AND (ItemPricingIsEnabled=@ItemPricingIsEnabled)";
            }
            DateTime now = DateTime.Now;

            SqlConnection cn  = GetConnection();
            SqlCommand    cmd = null;

            try
            {
                int dayOfWeek = (int)DayHelper.ConvertToDay(now.DayOfWeek);
                cmd = new SqlCommand(cmdText, cn);
                if (nowOnly)
                {
                    BuildSqlParameter(cmd, "@ItemPricingDayOfWeek", SqlDbType.TinyInt, dayOfWeek);
                    BuildSqlParameter(cmd, "@CurrentTime", SqlDbType.Time, now.TimeOfDay);
                }
                if (enableRequired)
                {
                    BuildSqlParameter(cmd, "@ItemPricingIsEnabled", SqlDbType.Bit, enableRequired);
                }
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        yield return(BuildItemPricing(rdr));
                    }
                }
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                FinishedWithConnection(cn);
            }
        }