private void button7_Click(object sender, EventArgs e) { int frid = Int32.Parse(textBox4.Text); int fiid = Int32.Parse(textBox8.Text); if (dbc.Ingredient_Record_Table.Count(r => ((r.RecipeID == frid) && (r.IngredientID == fiid))) != 0) { MessageBox.Show("已經有相同的食譜-食材關聯了"); } else { DialogResult result = MessageBox.Show("確認新增" + Environment.NewLine + "食譜:" + textBox5.Text + Environment.NewLine + "食材:" + textBox9.Text + Environment.NewLine + "單位:" + textBox12.Text + Environment.NewLine + "用量:" + textBox13.Text + Environment.NewLine + "的關聯性嗎?", "通知", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { var ingdr = new Ingredient_Record_Table() { RecipeID = Int32.Parse(textBox4.Text), IngredientID = Int32.Parse(textBox8.Text), Unt = textBox12.Text, IngredientAmountForUse = Int32.Parse(textBox13.Text), }; dbc.Ingredient_Record_Table.Add(ingdr); dbc.SaveChanges(); MessageBox.Show("新增成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); int rid = Int32.Parse(textBox4.Text); var ri = dbc.Ingredient_Record_Table.Where(i => i.RecipeID == rid).Select(r => new { r.RecipeID, r.IngredientID, RecordID = r.IngredientRecordID, Unit = r.Unt, Amt = r.IngredientAmountForUse, }).ToList(); bindingSource5.DataSource = ri; } else { return; } } }
private void btn_Update_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("確認更正?", "通知", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { if (tb_RecipeName.Text == null) { MessageBox.Show("未填寫食譜名稱", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (pb_RecipeIMG.Image == null) //判斷是否未上傳圖片 { MessageBox.Show("食譜圖片不得為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (tb_Howmany.Text == "" || tb_Time.Text == "") //判斷是否為空 { MessageBox.Show("人數及時間欄位不得為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (tb_RecipeDescription.Text == "") { MessageBox.Show("食譜描述不得為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (tb_Video.Text != "" && (!tb_Video.Text.Contains("youtu.be") && !tb_Video.Text.Contains("youtube"))) //判斷是否為Youtube網址 { MessageBox.Show("非YouTube網址", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (ID_List.Count == 0) { MessageBox.Show("食譜食材不得為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Step_List.Count == 0) { MessageBox.Show("步驟不可為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else //全部確認完成 { MemoryStream memory = new MemoryStream(); pb_RecipeIMG.Image.Save(memory, ImageFormat.Png); RecipeByte = memory.GetBuffer(); var Update = Delicious.Recipe_Table.AsEnumerable().Where(A => A.RecipeID == NowID).Select(A => A); foreach (var item in Update) { item.Picture = RecipeByte; item.RecipeName = tb_RecipeName.Text; item.RecipeDescription = tb_RecipeDescription.Text; item.RecipeCategoryID = cb_RecipeCategory.SelectedIndex + 1; item.ForHowMany = Convert.ToInt32(tb_Howmany.Text); item.TimeNeed = Convert.ToInt32(tb_Time.Text); item.Video = tb_Video.Text; } Delicious.SaveChanges(); var Update2 = Delicious.Ingredient_Record_Table.AsEnumerable().Where(B => B.RecipeID == NowID); Delicious.Ingredient_Record_Table.RemoveRange(Update2); Delicious.SaveChanges(); for (int i = 0; i < ID_List.Count; i++) { var Ingredient = new Ingredient_Record_Table() { IngredientID = ID_List[i], RecipeID = NowID, Unt = Unt_List[i], IngredientAmountForUse = ForUse_List[i], }; Delicious.Ingredient_Record_Table.Add(Ingredient); } Delicious.SaveChanges(); var Update3 = Delicious.Steps_Table.AsEnumerable().Where(C => C.RecipeID == NowID); Delicious.Steps_Table.RemoveRange(Update3); Delicious.SaveChanges(); for (int i = 0; i < Step_List.Count; i++) { var AddStep = new Steps_Table() { RecipeID = NowID, StepsNumber = i + 1, Steps = Step_List[i], Picture = StepImg_List[i] }; Delicious.Steps_Table.Add(AddStep); } Delicious.SaveChanges(); MessageBox.Show("修改成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } } }
private void btn_Save_Click(object sender, EventArgs e) { try { DialogResult result = MessageBox.Show("確認新增?", "通知", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { var Recipe = new Recipe_Table() { MemberID = Viewbag.member.MemberID, Picture = RecipeByte, RecipeName = tb_RecipeName.Text, RecipeCategoryID = cb_CategoryName.SelectedIndex + 1, ForHowMany = Convert.ToInt32(tb_howmany.Text), TimeNeed = Convert.ToInt32(tb_Time.Text) * 60, RecipeDescription = tb_RecipeDescription.Text, Video = tb_Video.Text, PostTime = DateTime.Now, ShareLink = "https://www.youtube.com/watch?v=072tU1tamd0", DeleteOrNot = false, DisVisible = false }; Delicious.Recipe_Table.Add(Recipe); Delicious.SaveChanges(); var NowRecipeID = (from A in Delicious.Recipe_Table select A.RecipeID).Max(); for (int i = 0; i < ID_List.Count; i++) { var Ingredient = new Ingredient_Record_Table() { IngredientID = ID_List[i], RecipeID = NowRecipeID, Unt = Unt_List[i], IngredientAmountForUse = ForUse_List[i], }; Delicious.Ingredient_Record_Table.Add(Ingredient); } Delicious.SaveChanges(); for (int i = 0; i < Step.Count; i++) { var AddStep = new Steps_Table() { RecipeID = NowRecipeID, StepsNumber = i + 1, Steps = Step[i], Picture = Img[i] }; Delicious.Steps_Table.Add(AddStep); } Delicious.SaveChanges(); MessageBox.Show("新增成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else { return; } } catch (Exception) { MessageBox.Show("新增失敗", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } }