Ejemplo n.º 1
0
        public List <Product> All()
        {
            string query = "SELECT * from Product";

            var model        = new List <Product>();
            var ImageContext = new ImageSQLContext();

            using (SqlConnection con = new SqlConnection(cs_database.CS()))
            {
                con.Open();
                SqlCommand    cmd    = new SqlCommand(query, con);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var p = new Product();
                    p.Id          = Convert.ToInt32(reader["Id"]);
                    p.Name        = (string)reader["Name"];
                    p.Ingredients = (string)reader["Ingredients"];
                    p.Price       = Convert.ToDouble(reader["Price"]);
                    p.Picture     = ImageContext.GetImageForProduct(Convert.ToInt32(reader["Id"]));
                    model.Add(p);
                }
            }
            return(model);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Populate an instance of <see cref="Product"/> from an <see cref="SqlDataReader"/>
        /// </summary>
        /// <param name="reader">The <see cref="SqlDataReader"/> containing a single record describing an <see cref="Product"/></param>
        /// <returns>An instance of <see cref="Product"/> populated with all available properties</returns>
        ///
        public static Product ProductFromReader(SqlDataReader reader)
        {
            //ProductSQLContext PContext = new ProductSQLContext();
            var     ImageContext = new ImageSQLContext();
            Product newproduct   = new Product
            {
                Id          = Convert.ToInt32(reader["Id"]),
                Name        = (string)reader["Name"],
                Ingredients = (string)reader["Ingredients"],
                Picture     = ImageContext.GetImageForProduct(Convert.ToInt32(reader["Id"]))
            };

            return(newproduct);
        }