4
 public static DataSet selectStudent()
 {
      MySqlConnection conn = connection();
      conn.Open();
      MySqlCommand cmd = new MySqlCommand();
      cmd.Connection = conn;
      MySqlDataAdapter adap = new MySqlDataAdapter(@"SELECT person.*, student.gradePointAverage, student.majorField FROM person JOIN student", conn);
      MySqlCommandBuilder sqlCmd = new MySqlCommandBuilder(adap);
      DataSet sqlSet = new DataSet();
      adap.Fill (sqlSet, "studentInfo");
      conn.Close();
      return sqlSet;
 }

和按钮:

 private void btnAdminStudentView_Click(object sender, EventArgs e)
 {
    DataSet ds = studentHelperClass.selectStudent();
    dataGridStudent.DataSource = ds.Tables["studentInfo"];
 }

为什么这会在按钮单击时给我这样的结果?

在此处输入图像描述

4

1 回答 1

6

您的查询产生笛卡尔积,因为您未能定义记录如何相互关联。你需要添加一个条件,

SELECT person.*, student.gradePointAverage, student.majorField 
FROM person JOIN student
     ON person.ID = student.ID  // example only

这意味着表中的记录与表person相关。IDstudent

要进一步了解有关联接的更多信息,请访问以下链接:

于 2013-09-16T12:10:12.377 回答