public static StudentModels select()
{
string sql = "select * from student";
SqlDataReader dr = DB.dr(sql);
if (dr.Read())
{
StudentModels sm = new StudentModels();
sm.Id = Convert.ToInt32(dr["id"]);
sm.Name = dr["name"].ToString();
sm.Sex = dr["sex"].ToString();
sm.Cm = ClassService.selectAll(dr["classID"].ToString());
dr.Close();
return sm;
}
public static ClassModels selectAll(string classID)
{
string sql = "select * from class where classID = '" + classID + "'";
SqlDataReader dr = DB.dr(sql);
if (dr.Read())
{
ClassModels cm = new ClassModels();
cm.Id = Convert.ToInt32(dr["id"]);
cm.ClassID = dr["classID"].ToString();
dr.Close();
return cm;
}
------解决方案--------------------------------------------------------
在SqlDataReader有两条以上的数据时
你这样取值 StudentModels 类里的成员只能取到student最后一条记录
建议用datatable