Exemplo n.º 1
0
        //Will update a current item in a list in the database.
        public static void UpdateItemInList(int itemId, string newName, int newQuantity)
        {
            GroceryItemsController.ItemValidityCheck(newName, newQuantity);

            try
            {
                SqlConnectionStringBuilder builder = DatabaseController.ConnectionBuilder();


                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {
                    connection.Open();
                    SqlCommand query = new SqlCommand(null, connection);

                    query.CommandText = "UPDATE dbo.Items SET itemName = @name, quantity = @quantity WHERE itemId = @Id;";

                    SqlParameter namePar     = new SqlParameter("@name", SqlDbType.VarChar, 255);
                    SqlParameter quantityPar = new SqlParameter("@quantity", SqlDbType.Int, 255);
                    SqlParameter idPar       = new SqlParameter("@Id", SqlDbType.Int, 255);

                    namePar.Value     = newName;
                    quantityPar.Value = newQuantity;
                    idPar.Value       = itemId;

                    query.Parameters.Add(namePar);
                    query.Parameters.Add(quantityPar);
                    query.Parameters.Add(idPar);

                    query.Prepare();
                    query.ExecuteNonQuery();
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.ToString());
            }

            return;
        }
Exemplo n.º 2
0
        //Will add a new item to a users lists in the the database.
        public static void AddItemToList(string name, int quantity, int listId)
        {
            GroceryItemsController.ItemValidityCheck(name, quantity);

            try
            {
                SqlConnectionStringBuilder builder = DatabaseController.ConnectionBuilder();

                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {
                    connection.Open();
                    SqlCommand query = new SqlCommand(null, connection);

                    query.CommandText = "INSERT INTO dbo.Items(itemName, quantity, listId) VALUES (@name, @quantity, @Id)";

                    SqlParameter namePar     = new SqlParameter("@name", SqlDbType.VarChar, 255);
                    SqlParameter quantityPar = new SqlParameter("@quantity", SqlDbType.Int, 255);
                    SqlParameter idPar       = new SqlParameter("@Id", SqlDbType.Int, 255);

                    namePar.Value     = name;
                    quantityPar.Value = quantity;
                    idPar.Value       = listId;

                    query.Parameters.Add(namePar);
                    query.Parameters.Add(quantityPar);
                    query.Parameters.Add(idPar);

                    query.Prepare();
                    query.ExecuteNonQuery();
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.ToString());
            }

            return;
        }