2012-08-06 53 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

相關問題