Example #1
0
        /// <summary>
        /// Retourne tous les articles de la base de données
        /// </summary>
        /// <returns></returns>
        public static Article[] GetAllArticles()
        {
            //Nombre d'articles dans la base de donnée
            int nbArticle = ArticleDAO.NbArticles();

            //Tableau d'artiles à retourner
            Article[] listToReturn = new Article[nbArticle];

            using (var Connection = new SQLiteConnection("Data Source = Bacchus.SQLite ;Version=3;New=False;Compress=True;"))
            {
                using (var Command = new SQLiteCommand("SELECT * FROM Articles;"))
                {
                    try
                    {
                        Command.Connection = Connection;
                        Command.Connection.Open();

                        using (SQLiteDataReader Reader = Command.ExecuteReader())
                        {
                            for (int currentArticleIndex = 0; currentArticleIndex < nbArticle; currentArticleIndex++)
                            {
                                // Lecture de la ligne
                                Reader.Read();

                                // Création d'un article
                                Article ArticleToAdd = new Article();

                                // Ajout des paramètres
                                ArticleToAdd.RefArticle   = Reader[0].ToString();
                                ArticleToAdd.Description  = Reader[1].ToString();
                                ArticleToAdd.RefSubFamily = SubFamilyDAO.GetSubFamilyById((int)Reader[2]);
                                ArticleToAdd.RefBrand     = BrandDAO.GetBrandById((int)Reader[3]);
                                ArticleToAdd.PriceHT      = float.Parse(Reader[4].ToString());
                                ArticleToAdd.Quantity     = (int)Reader[5];

                                // Ajout de l'article à la liste à retourner
                                listToReturn.SetValue(ArticleToAdd, currentArticleIndex);
                            }
                        }

                        Connection.Close();
                    }
                    catch (Exception ExceptionCaught)
                    {
                        //En cas d'erreur, retourne null
                        listToReturn = null;

                        MessageBox.Show("Echec de la récupération des données de la table Article \n" + ExceptionCaught.Message, ExceptionCaught.GetType().ToString());

                        Connection.Close();
                    }
                }
            }

            return(listToReturn);
        }
Example #2
0
        /// <summary>
        /// Retourne l'article correspondant à la référence passée en paramètre
        /// </summary>
        /// <param name="ArticleRef"></param>
        /// <returns></returns>
        public static Article GetArticleById(String ArticleRef)
        {
            Article ArticleToReturn = null;

            using (var Connection = new SQLiteConnection("Data Source = Bacchus.SQLite ;Version=3;New=False;Compress=True;"))
            {
                using (var Command = new SQLiteCommand("SELECT * FROM Articles WHERE RefArticle = '" + ArticleRef + "';"))
                {
                    try
                    {
                        Command.Connection = Connection;
                        Command.Connection.Open();

                        using (SQLiteDataReader Reader = Command.ExecuteReader())
                        {
                            // Lecture de la ligne
                            Reader.Read();

                            // Creation de l'article à retourner
                            ArticleToReturn = new Article();

                            // Ajout des paramètres
                            ArticleToReturn.RefArticle   = Reader[0].ToString();
                            ArticleToReturn.Description  = Reader[1].ToString();
                            ArticleToReturn.RefSubFamily = SubFamilyDAO.GetSubFamilyById((int)Reader[2]);
                            ArticleToReturn.RefBrand     = BrandDAO.GetBrandById((int)Reader[3]);
                            ArticleToReturn.PriceHT      = float.Parse(Reader[4].ToString());
                            ArticleToReturn.Quantity     = (int)Reader[5];
                        }

                        Connection.Close();
                    }
                    // Dans le cas où l'article n'existe pas
                    catch (InvalidOperationException)
                    {
                        ArticleToReturn = null;

                        Connection.Close();
                    }
                    catch (Exception ExceptionCaught)
                    {
                        ArticleToReturn = null;

                        MessageBox.Show("Echec de la récupération de l'article " + ArticleRef + " \n" + ExceptionCaught.Message, ExceptionCaught.GetType().ToString());

                        Connection.Close();
                    }
                }
            }

            return(ArticleToReturn);
        }
Example #3
0
        /// <summary>
        /// Retourne toutes les marques de la base de données
        /// </summary>
        /// <returns></returns>
        public static Brand[] GetAllBrands()
        {
            //The number of brands to get
            int NbBrands = BrandDAO.NbBrands();

            //The table to return
            Brand[] listToReturn = new Brand[NbBrands];

            using (var Connection = new SQLiteConnection("Data Source = Bacchus.SQLite ;Version=3;New=False;Compress=True;"))
            {
                using (var Command = new SQLiteCommand("SELECT * FROM Marques;"))
                {
                    try
                    {
                        Command.Connection = Connection;
                        Command.Connection.Open();

                        using (SQLiteDataReader Reader = Command.ExecuteReader())
                        {
                            for (int currentBrandIndex = 0; currentBrandIndex < NbBrands; currentBrandIndex++)
                            {
                                Reader.Read();

                                Brand BrandToAdd = new Brand();

                                BrandToAdd.RefBrand  = (int)Reader[0];
                                BrandToAdd.NameBrand = Reader[1].ToString();

                                listToReturn.SetValue(BrandToAdd, currentBrandIndex);
                            }
                        }

                        Connection.Close();
                    }
                    catch (Exception ExceptionCaught)
                    {
                        // Retourne null en cas d'erreur
                        listToReturn = null;

                        MessageBox.Show("Echec de la récupération des données de la table Marques  \n" + ExceptionCaught.Message, ExceptionCaught.GetType().ToString());

                        Connection.Close();
                    }
                }
            }

            return(listToReturn);
        }