コード例 #1
0
        public static List<Student> GetAllStudents()
        {
            using (MyContext context = new MyContext())
            {
                var query = from p in context.Students
                            orderby p.StudentID descending
                            select p;

                return query.ToList();

            }
        }
コード例 #2
0
        public static Student GetStudentByID(int sid)
        {
            using (MyContext context = new MyContext())
            {
                var query = from p in context.Students
                            where p.StudentID == sid
                            select p;

                return query.FirstOrDefault();

            }
        }
コード例 #3
0
        public static void DeleteStudentByID(int sid)
        {
            using (MyContext context = new MyContext())
            {
                var query = from p in context.Students
                            where p.StudentID == sid
                            select p;

                var std = query.FirstOrDefault();

                context.Students.Remove(std);
                context.SaveChanges();

            }
        }
コード例 #4
0
        public static int SaveStudent(Student stdObj)
        {
            using (MyContext context = new MyContext())
            {
                if (stdObj.StudentID > 0)
                {
                    var std = context.Students.Where(p => p.StudentID == stdObj.StudentID).FirstOrDefault();
                    std.Name = stdObj.Name;
                    std.Address = stdObj.Address;
                    std.Age = stdObj.Age;

                }
                else
                {
                    context.Students.Add(stdObj);
                }
                context.SaveChanges();
            }

            return stdObj.StudentID;
        }