コード例 #1
0
        public IReadOnlyList <Bestelling> GeefBestellingen()
        {
            _bestellingen.Clear();
            SqlConnection connection = getConnection();
            string        bQuery     = "SELECT * FROM dbo.bestelling b ORDER BY b.klantID";
            string        pQuery     = "SELECT p.*,bd.aantal FROM dbo.bestellingDetails bd"
                                       + " JOIN dbo.product p ON p.productID = bd.productID"
                                       + " WHERE bd.bestellingID = @bestellingID";
            string kQuery = "SELECT * FROM dbo.klant k WHERE k.klantID = @klantID";

            using (SqlCommand command1 = connection.CreateCommand())
                using (SqlCommand command2 = connection.CreateCommand())
                    using (SqlCommand command3 = connection.CreateCommand())
                    {
                        connection.Open();
                        SqlTransaction transaction = connection.BeginTransaction();
                        command1.Transaction = transaction;
                        command2.Transaction = transaction;
                        command3.Transaction = transaction;
                        try
                        {
                            command1.CommandText = bQuery;
                            SqlDataReader bReader = command1.ExecuteReader();
                            command2.Parameters.Add(new SqlParameter("bestellingID", SqlDbType.Int));
                            command3.Parameters.Add(new SqlParameter("klantID", SqlDbType.Int));

                            Klant bestaandeKlant = null;

                            while (bReader.Read())
                            {
                                Dictionary <Product, int> _producten = new Dictionary <Product, int>();
                                command2.Parameters["bestellingID"].Value = (int)bReader["bestellingID"];
                                command2.CommandText = pQuery;
                                SqlDataReader pReader = command2.ExecuteReader();
                                while (pReader.Read())
                                {
                                    _producten.Add(new Product((int)pReader["productID"], (string)pReader["naam"], (double)pReader["prijs"]), (int)pReader["aantal"]);
                                }
                                pReader.Close();

                                command3.CommandText = kQuery;
                                command3.Parameters["klantID"].Value = (int)bReader["klantID"];
                                SqlDataReader kReader = command3.ExecuteReader();
                                kReader.Read();
                                Klant klant = new Klant((int)bReader["klantID"], (string)kReader["naam"], (string)kReader["adres"]);
                                kReader.Close();

                                //indien het de eerste keer door de while lus gaat wordt de klant gelink aan bestaande klant
                                if (bestaandeKlant == null)
                                {
                                    bestaandeKlant = klant;
                                }
                                //indien de klant niet gelijk is aan de bestaande klant wordt de bestaande klant aangepast, op die manier worden de bestellingen aan de lijst van
                                //bestelling van de klant toegevoegd indien de klant dus al bestaat
                                if (!bestaandeKlant.Equals(klant))
                                {
                                    bestaandeKlant = klant;
                                }

                                Bestelling bestelling = new Bestelling((int)bReader["bestellingID"], bestaandeKlant, (DateTime)bReader["tijdstip"], _producten);
                                //check of bestelling betaald is, zoja -> zet betaald
                                if ((Boolean)bReader["betaald"])
                                {
                                    bestelling.ZetBetaald();
                                }
                                _bestellingen.Add(bestelling.BestellingId, bestelling);
                            }
                            bReader.Close();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                            return(null);
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }
            return(new List <Bestelling>(_bestellingen.Values).AsReadOnly());
        }