コード例 #1
0
 public void CreateStudent(Student student)
 {
     using (SchoolDB db = new SchoolDB())
     {
         db.Student.Add(student);
         db.SaveChanges();
     }
 }
コード例 #2
0
        public void UpdateStudent(StudentData student)
        {
            using (SchoolDB db = new SchoolDB())
            {
                var EditedObj = db.Student.Find(student.ID);

                if (EditedObj != null)//if student is found
                {
                    EditedObj.StudentName = student.Name;
                    db.SaveChanges();
                }
            }
        }
コード例 #3
0
        public StudentData GetStudent(int StudentID)
        {
            var student = new StudentData();

            using (SchoolDB db = new SchoolDB())
            {
                var studentDB = db.Student.Find(StudentID);

                student.ID   = studentDB.StudentID;
                student.Name = studentDB.StudentName;
            }

            return(student);
        }
コード例 #4
0
        public void DeleteStudent(int StudentID)
        {
            using (SchoolDB db = new SchoolDB())
            {
                var EditedObj = db.Student.Find(StudentID);

                if (EditedObj != null)
                {
                    var classes = db.Class.Where(x => x.StudentID == StudentID);

                    foreach (var classe in classes)
                    {
                        classe.StudentID = null;
                    }

                    db.Student.Remove(EditedObj);
                    db.SaveChanges();
                }
            }
        }
コード例 #5
0
        public List <StudentData> GetStudents()
        {
            var lista = new List <StudentData>();

            using (SchoolDB db = new SchoolDB())
            {
                //LINQ query to get students
                var listaDB = (from p in db.Student
                               select p).ToList();

                foreach (var item in listaDB)
                {
                    lista.Add(new StudentData()
                    {
                        ID = item.StudentID, Name = item.StudentName
                    });
                }
            }

            return(lista);
        }