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();
       }

値を取りません....どうすればいいですか?

4

1 に答える 1

1

SELECTステートメントが行を返さない場合は、データに問題があります。テーブルEnrollmentTable.StudentID内のどのレコードとも一致しないか、テーブル内のどのコースとも一致しません。StudentEnrollmentTable.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; 
}
于 2012-08-06T20:16:04.177 に答える