コード例 #1
0
        private void LoadOffers(BasketLibrary.ShoppingBasket basket)
        {
            // Load offers from database
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Supermarket;Integrated Security=True";
                conn.Open();
                SqlCommand command = new SqlCommand("SELECT * FROM dbo.Offers", conn);

                // Loop through each row in the results
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // Convert potential null in columns to be 0 to avoid type error
                        int realGroup    = reader[3] == DBNull.Value ? 0 : Convert.ToInt32(reader[3]);
                        int realDiscount = reader[4] == DBNull.Value ? 0 : Convert.ToInt32(reader[4]);

                        // Add offer to the list of offers
                        Offer newOffer = new Offer(
                            Convert.ToInt32(reader[0]), (string)reader[1], (string)reader[2]
                            , realGroup, realDiscount);
                        basket.Offers.Add(newOffer);
                    }
                }
            }
        }
コード例 #2
0
        private void LoadProducts(BasketLibrary.ShoppingBasket basket)
        {
            // Load products from database
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Supermarket;Integrated Security=True";
                conn.Open();

                // Read rows from products table
                SqlCommand command = new SqlCommand("SELECT * FROM dbo.Products", conn);

                // Loop through each row in the results
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // Add a unique product for each row, setting quantity to 0
                        // get offer details for each product
                        string productName = (String)reader[2];
                        int    realOfferId = reader[1] == DBNull.Value ? 0 : Convert.ToInt32(reader[1]);

                        basket.AddProduct(productName, 0, Convert.ToDouble(reader[3]), realOfferId);

                        // Add product name to drop down list of options
                        productComboBox.Items.Add(productName);
                    }
                }
            }
        }
コード例 #3
0
        private void loadBasketData(BasketLibrary.ShoppingBasket basket)
        {
            LoadOffers(basket);
            LoadProducts(basket);

            // Set selected item of product combo box to be the first entry
            productComboBox.SelectedIndex = 0;
        }