public ActionResult Index() { var c1 = new Course("1", "C#"); var c2 = new Course("2", "Java"); var c3 = new Course("3", "VBA"); var s1 = new Student("1", "Halo"); var s2 = new Student("2", "lalalal"); var s3 = new Student("3", "Alien"); var sc1 = new StuCourse(s1, new List <Course>() { c1, c2, c3 }); var sc2 = new StuCourse(s2, new List <Course>() { c2, c3 }); var sc3 = new StuCourse(s3, new List <Course>() { c1, c3 }); return(View(new List <StuCourse>() { sc1, sc2, sc3 })); }
public ActionResult Create([Bind(Include = "StudentID,CourseID")] StuCourse schoolinfo) { if (ModelState.IsValid) { //schoolinfo.Misc = "1"; db.StuCourses.Add(schoolinfo); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(schoolinfo)); }
public void TestAddStudentAccess() { // 创建实体对象 Class clazz = new Class(); clazz.Id = 10000; clazz.ClassName = "信息工程"; Course course = new Course(); course.Id = 20000; course.CourseName = "数据库原理"; Student student = new Student(); student.Id = 30000; student.StuName = "Vincent"; student.StuAge = 20; // 手动设置关联关系 student.ClassId = clazz.Id; StuCourse stuCourse = new StuCourse(); stuCourse.StuId = student.Id; stuCourse.CourseId = course.Id; // 拼接SQL进行操作数据库 OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=StuCourse.mdb"); conn.Open(); string sql = "insert into class (id, class_name) values (" + clazz.Id + ", '" + clazz.ClassName + "')"; OleDbCommand cmd = new OleDbCommand(sql, conn); cmd.ExecuteNonQuery(); sql = "insert into course (id, course_name) values (" + course.Id + ", '" + course.CourseName + "')"; cmd = new OleDbCommand(sql, conn); cmd.ExecuteNonQuery(); sql = "insert into student (id, stu_name, stu_age, class_id) " + "values (" + student.Id + ", '" + student.StuName + "', " + student.StuAge + ", " + student.ClassId + ")"; cmd = new OleDbCommand(sql, conn); cmd.ExecuteNonQuery(); sql = "insert into stu_course (stu_id, course_id) values (" + stuCourse.StuId + ", " + stuCourse.CourseId + ")"; cmd = new OleDbCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); }
public void TestAddStudent() { // 创建实体对象 Class clazz = new Class(); clazz.Id = 10000; clazz.ClassName = "信息工程"; Course course = new Course(); course.Id = 20000; course.CourseName = "数据库原理"; Student student = new Student(); student.Id = 30000; student.StuName = "Vincent"; student.StuAge = 20; // 手动设置关联关系 student.ClassId = clazz.Id; StuCourse stuCourse = new StuCourse(); stuCourse.StuId = student.Id; stuCourse.CourseId = course.Id; // 拼接SQL进行操作数据库 MySqlConnection conn = new MySqlConnection("server=localhost;database=test;user id=root;password=root"); conn.Open(); string sql = "insert into class (id, class_name) values (" + clazz.Id + ", '" + clazz.ClassName + "')"; MySqlCommand cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); sql = "insert into course (id, course_name) values (" + course.Id + ", '" + course.CourseName + "')"; cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); sql = "insert into student (id, stu_name, stu_age, class_id) " + "values (" + student.Id + ", '" + student.StuName + "', " + student.StuAge + ", " + student.ClassId + ")"; cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); sql = "insert into stu_course (stu_id, course_id) values (" + stuCourse.StuId + ", " + stuCourse.CourseId + ")"; cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); }