示例#1
0
        public void Update(string newName, string newInstruction, int newRating)
        {
            SqlConnection conn = DB.Connection();

            conn.Open();

            SqlCommand cmd = new SqlCommand("UPDATE recipes SET name = @NewName, instruction = @NewInstruction, rating = @NewRating WHERE recipes.id = @TargetId;", conn);

            cmd.Parameters.Add(new SqlParameter("@NewName", newName));
            cmd.Parameters.Add(new SqlParameter("@NewInstruction", newInstruction));
            cmd.Parameters.Add(new SqlParameter("@NewRating", newRating.ToString()));
            cmd.Parameters.Add(new SqlParameter("@TargetId", this.GetId()));
            cmd.ExecuteNonQuery();

            this.SetName(newName);
            this.SetInstruction(newInstruction);
            this.SetRating(newRating);

            DB.CloseSqlConnection(conn);
        }
示例#2
0
        public void Save()
        {
            int potentialId = this.IsNewCategory();

            if (potentialId == -1)
            {
                SqlConnection conn = DB.Connection();
                conn.Open();

                SqlCommand cmd = new SqlCommand("INSERT INTO categories (name) OUTPUT INSERTED.id VALUES (@CategoryName) ;", conn);
                cmd.Parameters.Add(new SqlParameter("@CategoryName", this.GetName()));
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    potentialId = rdr.GetInt32(0);
                }

                DB.CloseSqlConnection(conn, rdr);
            }
            this.SetId(potentialId);
        }
示例#3
0
        public static List <Category> GetAll()
        {
            List <Category> allCategories = new List <Category> {
            };
            SqlConnection conn            = DB.Connection();

            conn.Open();

            SqlCommand    cmd = new SqlCommand("SELECT * FROM categories;", conn);
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                int      categoryId   = rdr.GetInt32(0);
                string   categoryName = rdr.GetString(1);
                Category newCategory  = new Category(categoryName, categoryId);
                allCategories.Add(newCategory);
            }

            DB.CloseSqlConnection(conn, rdr);
            return(allCategories);
        }
示例#4
0
        public int IsNewCategory()
        {
            // Checks to see if an ingredient exists in the database already. If it does, returns the id. Otherwise, returns -1
            SqlConnection conn = DB.Connection();

            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT id FROM categories WHERE name = @CategoryName", conn);

            cmd.Parameters.Add(new SqlParameter("@CategoryName", this.GetName()));
            SqlDataReader rdr = cmd.ExecuteReader();

            int foundId = -1;

            while (rdr.Read())
            {
                foundId = rdr.GetInt32(0);
            }

            DB.CloseSqlConnection(conn, rdr);
            return(foundId);
        }
示例#5
0
        public static List <Ingredient> GetAll()
        {
            List <Ingredient> allIngredients = new List <Ingredient> {
            };
            SqlConnection conn = DB.Connection();

            conn.Open();

            SqlCommand    cmd = new SqlCommand("SELECT * FROM ingredients;", conn);
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                int        ingredientId   = rdr.GetInt32(0);
                string     ingredientName = rdr.GetString(1);
                Ingredient newIngredient  = new Ingredient(ingredientName, ingredientId);
                allIngredients.Add(newIngredient);
            }

            DB.CloseSqlConnection(conn, rdr);
            return(allIngredients);
        }
示例#6
0
        public static List <Category> SearchName(string categoryName)
        {
            List <Category> foundCategories = new List <Category> {
            };
            SqlConnection conn = DB.Connection();

            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT * FROM categories WHERE name LIKE @CategoryName", conn);

            cmd.Parameters.Add(new SqlParameter("@CategoryName", "%" + categoryName + "%"));
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                int      categoryId    = rdr.GetInt32(0);
                string   newName       = rdr.GetString(1);
                Category foundCategory = new Category(newName, categoryId);
                foundCategories.Add(foundCategory);
            }

            DB.CloseSqlConnection(conn, rdr);
            return(foundCategories);
        }
示例#7
0
        public List <Category> GetCategory()
        {
            List <Category> allCategories = new List <Category> {
            };
            SqlConnection conn            = DB.Connection();

            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT categories.* FROM ingredients JOIN ingredients_categories ON (ingredients.id = ingredients_categories.ingredient_id) JOIN categories ON (categories.id = ingredients_categories.category_id) WHERE ingredients.id = @IngredientId;", conn);

            cmd.Parameters.Add(new SqlParameter("@IngredientId", this.GetId().ToString()));
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                int      Id           = rdr.GetInt32(0);
                string   categoryName = rdr.GetString(1);
                Category newCategory  = new Category(categoryName, Id);
                allCategories.Add(newCategory);
            }

            DB.CloseSqlConnection(conn, rdr);
            return(allCategories);
        }
示例#8
0
        public static List <Ingredient> SearchName(string ingredientName)
        {
            List <Ingredient> foundIngredients = new List <Ingredient> {
            };
            SqlConnection conn = DB.Connection();

            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT * FROM ingredients WHERE name LIKE @IngredientName", conn);

            cmd.Parameters.Add(new SqlParameter("@IngredientName", "%" + ingredientName + "%"));
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                int        ingredientId    = rdr.GetInt32(0);
                string     newName         = rdr.GetString(1);
                Ingredient foundIngredient = new Ingredient(newName, ingredientId);
                foundIngredients.Add(foundIngredient);
            }

            DB.CloseSqlConnection(conn, rdr);
            return(foundIngredients);
        }
示例#9
0
        public static List <Recipe> GetAll()
        {
            List <Recipe> allRecipes = new List <Recipe> {
            };
            SqlConnection conn       = DB.Connection();

            conn.Open();

            SqlCommand    cmd = new SqlCommand("SELECT * FROM recipes ORDER BY rating DESC;", conn);
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                int    recipeId     = rdr.GetInt32(0);
                string recipeName   = rdr.GetString(1);
                string instruction  = rdr.GetString(2);
                int    recipeRating = rdr.GetInt32(3);
                Recipe newRecipe    = new Recipe(recipeName, instruction, recipeRating, recipeId);
                allRecipes.Add(newRecipe);
            }

            DB.CloseSqlConnection(conn, rdr);
            return(allRecipes);
        }
示例#10
0
        public void Save()
        {
            int potentialId = this.IsNewRecipe();

            if (potentialId == -1)
            {
                SqlConnection conn = DB.Connection();
                conn.Open();

                SqlCommand cmd = new SqlCommand("INSERT INTO recipes (name, instruction, rating) OUTPUT INSERTED.id VALUES (@RecipeName, @Instruction, @Rating) ;", conn);
                cmd.Parameters.Add(new SqlParameter("@RecipeName", this.GetName()));
                cmd.Parameters.Add(new SqlParameter("@Instruction", this.GetInstruction()));
                cmd.Parameters.Add(new SqlParameter("@Rating", this.GetRating()));
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    potentialId = rdr.GetInt32(0);
                }

                DB.CloseSqlConnection(conn, rdr);
            }
            this.SetId(potentialId);
        }