2012-08-06 86 views
0

我创建了3个类,然后这3个类的对象在另一个类中创建。现在我想从数据库中读取数据。 我的代码是:我从数据库中检索数据并从组合框中调用。如何从另一个对象中的一个对象检索数据?

 SqlConnection conn = MyDB.GetConnection(); 
     string selectStm = "SELECT c.CourseID,en.Score,s.StudentID FROM EnrollmentTable en,Course c,Student s WHERE en.StudentID = s.StudentID AND en.CourseID = c.CourseID"; 

     SqlCommand command = new SqlCommand(selectStm, conn); 
     //command.Parameters.AddWithValue("@StudentID", StudentID); 


     List<StudentScore> aStudentScore = new List<StudentScore>(); 

     StudentScore score = null; 



     try 
     { 
      conn.Open(); 
      SqlDataReader reader = command.ExecuteReader(); 


      Enrollment enr = new Enrollment(); 

      while (reader.Read()) 
      { 
       score = new StudentScore(); 



       score.EnrollmentData.StudentData.StudentID = reader["StudentID"].ToString();//Here is the problem. 
       score.EnrollmentData.CourseData.CourseID = reader["CourseID"].ToString();//Here is the problem. 

       score.Score = Convert.ToInt32(reader["Score"]); 



       aStudentScore.Add(score); 
      } 

      reader.Close(); 
      return aStudentScore; 
     } 
     catch (SqlException ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      conn.Close(); 
     } 

它没有任何价值......我该怎么做?

+0

什么不采取任何价值? – XORcist 2012-08-06 18:57:06

+0

我的意思是,我想从表中检索数据它不检索数据... – user1050667 2012-08-06 19:03:08

+0

什么?什么显示空?你可以发布异常追溯? – XORcist 2012-08-06 19:04:44

回答

1

如果您的SELECT声明没有返回任何行,那么您的数据存在问题。 EnrollmentTable.StudentIDStudent表中的任何记录不匹配,或者EnrollmentTable.CourseIDCourse表中的任何课程不匹配。

在任何情况下,我试图简化代码,所以它会更容易阅读:

public class Student 
{ 
    string studentID = string.Empty; 
    public string StudentID 
    {get { return studentID;} 
    set { studentID = value;}} 
}; 

public class Course 
{ 
    string courseID = string.Empty; 
    public string CourseID 
    {get { return courseID;} 
    set { courseID = value;}} 
}; 

public class Enrollment 
{ 
    Student studentData = new Student(); 
    public Student StudentData 
    {get { return studentData;} 
    set { studentData = value;} 
    } 
    Course courseData = new Course(); 
    public Course CourseData 
    {get { return courseData; } 
    set { courseData = value; }} 
}; 

public class StudentScore 
{ 
    Enrollment enrollmentData = new Enrollment(); 
    public Enrollment EnrollmentData 
    {get { return enrollmentData;} 
    set { enrollmentData = value;}} 

    int score = 0; 
    public int Score 
    {get {return score;} 
    set {score = value;}} 
}; 

public List<StudentScore> getScoreList() 
{ 
    List<StudentScore> aStudentScore = new List<StudentScore>(); 
    StringBuilder tmpSQL = new StringBuilder(); 
    tmpSQL.Append("SELECT c.CourseID, en.Score, s.StudentID "); 
    tmpSQL.Append("FROM EnrollmentTable en "); 
    tmpSQL.Append("  INNER JOIN Student s ON en.StudentID = s.StudentID "); 
    tmpSQL.Append("  INNER JOIN Course c ON en.CourseID = c.CourseID "); 
    try 
    {using (SqlConnection conn = MyDB.GetConnection()){ 
     conn.Open(); 
     using (SqlCommand command = new SqlCommand(tmpSQL.ToString(), conn)){ 
     using (SqlDataReader reader = command.ExecuteReader()){ 
      while (reader.Read()) 
      { 
       StudentScore score = new StudentScore(); 
       score.EnrollmentData.StudentData.StudentID = reader["StudentID"].ToString(); 
       score.EnrollmentData.CourseData.CourseID = reader["CourseID"].ToString(); 
       score.Score = Convert.ToInt32(reader["Score"]); 
       aStudentScore.Add(score); 
       }; 
      }; 
     }; 
     }; 
    } 
    catch (Exception ex) 
    {throw ex;} 
    return aStudentScore; 
} 
+0

@ user1050667,如果你得到0行返回,那么数据或你的数据结构有问题,SELECT语句是非常直的如果不是,那么你需要查看EnrollmentTable,Student表和Course表中的数据,看他们是否手动匹配(也许一个是VarChar和其他是整数或浮点数?) – 2012-08-06 21:23:20

+0

每一件事情都匹配,但它仍然显示空值,我认为我们给路径如下:** score.EnrollmentData.StudentData.StudentID = reader [“StudentID”]。ToString() **我认为这一个有一个问题,它显示为空。 – user1050667 2012-08-07 14:07:40

+0

@ user1050667,正如之前的评论员所建议的那样,您是否在SQL Server Management Studio中运行查询以查看结果是什么?如果是这样,发布一些结果。 – 2012-08-07 14:13:07

相关问题