Example #1
0
        public Product GetProdut(int id)
        {
            var query = $"SELECT * FROM tableproduct WHERE Id={id}";

            try
            {
                conn = DatabaseConn.CreateConnection();
                var cmd    = new MySqlCommand(query, conn);
                var reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    var product = new Product
                    {
                        Id       = reader.GetInt32(0),
                        Name     = reader.GetString(1),
                        Price    = reader.GetDouble(2),
                        Category = reader.GetString(3)
                    };
                    return(product);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }
Example #2
0
        public List <Product> GetAllProducts()
        {
            var query = "SELECT * FROM tableproduct";

            try
            {
                conn = DatabaseConn.CreateConnection();
                var cmd        = new MySqlCommand(query, conn);
                var reader     = cmd.ExecuteReader();
                var productLst = new List <Product>();
                while (reader.Read())
                {
                    var product = new Product
                    {
                        Id       = reader.GetInt32(0),
                        Name     = reader.GetString(1),
                        Price    = reader.GetDouble(2),
                        Category = reader.GetString(3)
                    };
                    productLst.Add(product);
                }

                return(productLst);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Example #3
0
        public bool DeleteProduct(int id)
        {
            var query = $"SELECT * FROM tableproduct WHERE Id={id}";

            try
            {
                conn = DatabaseConn.CreateConnection();
                var cmd    = new MySqlCommand(query, conn);
                var reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    reader.Close();
                    query = $"DELETE FROM tableproduct WHERE Id={id}";
                    var cmd2 = new MySqlCommand(query, conn);
                    cmd2.ExecuteNonQuery();
                    return(true);
                }

                return(false);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }
Example #4
0
        public bool UpdateProduct(int id, Product product)
        {
            var query = $"SELECT * FROM tableproduct WHERE Id={id}";

            try
            {
                conn = DatabaseConn.CreateConnection();
                var cmd    = new MySqlCommand(query, conn);
                var reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    reader.Close();
                    query =
                        $"UPDATE tableproduct SET Name='{product.Name}', Price='{product.Price}', Category='{product.Category}' WHERE Id={id}";
                    var cmd2 = new MySqlCommand(query, conn);
                    cmd2.ExecuteNonQuery();
                    return(true);
                }

                return(false);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }
Example #5
0
        public int CreateProduct(Product product)
        {
            var query =
                $"INSERT INTO tableproduct (Name, Price, Category) VALUES ('{product.Name}',{product.Price},'{product.Category}')";

            try
            {
                conn = DatabaseConn.CreateConnection();
                var cmd = new MySqlCommand(query, conn);
                cmd.ExecuteNonQuery();
                return(Convert.ToInt32(cmd.LastInsertedId));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }