public static void UpdateData() { try { using (var context = new NewDBContext()) { Show("tochange"); Console.WriteLine("Choose ID to update"); var markId = int.Parse(Console.ReadLine()); var row = context.MarkList.Find(markId); if (row != null) { Console.WriteLine("Would you like to change student name? (Y(yes)/N(no))"); if ((Console.ReadLine()).ToUpper() == "Y") { ShowStudentIdAndName(); Console.Write("new ID: "); var studId = int.Parse(Console.ReadLine()); row.StudentId = studId; } Console.WriteLine("Would you like to change course? (Y(yes)/N(no))"); if ((Console.ReadLine()).ToUpper() == "Y") { ShowCourseIdAndName(); Console.Write("new course ID: "); var courseId = int.Parse(Console.ReadLine()); row.CourseId = courseId; } Console.WriteLine("Would you like to change mark? (Y(yes)/N(no))"); if ((Console.ReadLine()).ToUpper() == "Y") { Console.Write("new mark from 2-4: "); var point = int.Parse(Console.ReadLine()); row.Point = point; } if (context.SaveChanges() > 0) { Console.WriteLine("Data was successfully updated!"); } else { Console.WriteLine("Fail! Data wasn't updated!"); } } else { Console.WriteLine("Wrong mark ID!"); } } } catch (Exception x) { Console.WriteLine($"Fail:{x.Message}"); } finally { Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } }
public static void DeleteData() { try { using (var context = new NewDBContext()) { Show("tochange"); Console.Write("Choose ID to delete:"); var markId = int.Parse(Console.ReadLine()); var row = context.MarkList.Find(markId); if (row != null) { Console.Write("Are you sure? Y(yes)/N(no):"); var confirm = Console.ReadLine(); if (confirm.ToUpper() == "Y") { context.MarkList.Remove(row); } if (context.SaveChanges() > 0) { Console.WriteLine("Data was successfully deleted!"); } else { Console.WriteLine("Fail! Data wasn't deleted!"); } } else { Console.WriteLine("Wrong mark ID!"); } } } catch (Exception x) { Console.WriteLine($"Fail:{x.Message}"); } finally { Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } }
public static void AddData() { try { using (var context = new NewDBContext()) { Console.WriteLine("Choose Student id: "); ShowStudentIdAndName(); var studId = int.Parse(Console.ReadLine()); Console.WriteLine("Choose Course id: "); ShowCourseIdAndName(); var courseId = int.Parse(Console.ReadLine()); Console.WriteLine("Choose mark from 2-4:"); var point = int.Parse(Console.ReadLine()); MarkList newMark = new MarkList() { StudentId = studId, CourseId = courseId, Point = point }; context.MarkList.Add(newMark); var result = context.SaveChanges(); if (result > 0) { Console.WriteLine("New data was successfully added!"); } } } catch (Exception x) { Console.WriteLine($"Fail:{x.Message}"); } finally { Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } }