private void BtAdd_Click(object sender, EventArgs e) { string name; string cals; string amount; string id; string cost; if (tbName.Text.Length > 0) { name = tbName.Text; } else { name = "none"; } if (DBOps.isReal(tbCals.Text)) { cals = tbCals.Text; } else { cals = "0.0"; } if (DBOps.isReal(tbAmount.Text)) { amount = tbAmount.Text; } else { amount = "0.0"; } if (DBOps.isInteger(tbCost.Text)) { cost = tbCost.Text; } else { cost = "0"; } if (cbUnits.Text != "") { id = cbUnits.SelectedIndex.ToString(); } else { id = "0"; } string Query = $"INSERT INTO Products ('Name', 'Calories', 'Amount', 'UnitID', 'Cost') values" + $" ('{name}','{cals}','{amount}','{id}','{cost}')"; DBOps.WriteDB(Query); }
private void BtAddRecipe_Click(object sender, EventArgs e) { if (tbName.Text == "") { MessageBox.Show("Имя не может быть пустым.", "Ошибка"); return; } if (!cbEvening.Checked & !cbMorning.Checked & !cbNoon.Checked) { MessageBox.Show("Выберите хоть одно время суток.", "Ошибка"); return; } if (lbProducts.Items.Count == 0) { MessageBox.Show("В рецепте должен быть хотя бы один продукт.", "Ошибка"); return; } if (cbCategory.SelectedItem == null) { MessageBox.Show("Выберите категорию.", "Ошибка"); return; } string RecipeName = tbName.Text; bool[] Times = { cbMorning.Checked, cbNoon.Checked, cbEvening.Checked }; string RecipeText = tbDescription.Text; string RecipeQuery = $"INSERT INTO Recipes ('Name','RecipeText','isMorning','isNoon','isEvening')" + $"VALUES ('{RecipeName}','{RecipeText}','{Times[0]}','{Times[1]}','{Times[2]}')"; // Sending recipe to db DBOps.WriteDB(RecipeQuery); DataTable cache = DBOps.ReadDB("SELECT ID FROM Recipes"); string recipeid = cache.Rows[cache.Rows.Count - 1].ItemArray[0].ToString(); for (int i = 0; i < products.Length; i++) { if (products[i].Used) { string importance = products[i].Importance.ToString(); string localWeight = products[i].LocalWeight.ToString(); string productid = products[i].ID.ToString(); string WeightsQuery = $"INSERT INTO Weights ('RecipeID','ProductID','Weight','Importance')" + $"VALUES('{recipeid}','{productid}','{localWeight}','{importance}')"; DBOps.WriteDB(WeightsQuery); } } }