/// <summary> /// 获取前20条学生数据及其班级信息 /// </summary> /// <returns></returns> public List<Student> GetTop20Data() { List<Student> list = new List<Student>(); string sql = "select top 20 MyStudent.*,MyClass.className from MyStudent inner join MyClass on MyStudent.FClassId=MyClass.classId"; //Fid FName FAge FGender FMath FEnglish FClassId FBirthday className using (SqlDataReader reader=SqlHelper.ExecuteReader(sql, System.Data.CommandType.Text)) { if (reader.HasRows) { while (reader.Read()) { Student s = new Student(); s.StudentId = reader.GetInt32(0); s.Name = reader.GetString(1); s.Age = reader.GetInt32(2); s.Gender = reader.GetString(3); s.MathScore = reader.IsDBNull(4) ? null : (int?)reader.GetInt32(4); s.EnglishScore = reader.GetInt32(5); s.Birthday = reader.GetDateTime(7); StudentClass studentClass = new StudentClass(); studentClass.ClassId = reader.GetInt32(6); studentClass.ClassName = reader.GetString(8); s.Myclass = studentClass; list.Add(s); } } } return list; }
/// <summary> /// 获得所有的班级信息 /// </summary> /// <returns></returns> public List<StudentClass> GetClassMsg() { List<StudentClass> list = new List<StudentClass>(); string sql = "select * from MyClass"; using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, System.Data.CommandType.Text)) { if (reader.HasRows) { while (reader.Read()) { StudentClass sClass = new StudentClass(); sClass.ClassId = reader.GetInt32(0); sClass.ClassName = reader.GetString(1); list.Add(sClass); } } } return list; }