C# 为什么我的选择与dataGrid重复?
和按钮:C# 为什么我的选择与dataGrid重复?,c#,mysql,C#,Mysql,和按钮: public static DataSet selectStudent() { MySqlConnection conn = connection(); conn.Open(); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; MySqlDataAdapter adap = new MySqlDataAdapter(@"SELECT person.
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"];
}
这意味着来自表person
的记录与表student
上的ID
相关
要进一步了解有关联接的更多信息,请访问以下链接:
SELECT person.*, student.gradePointAverage, student.majorField
FROM person JOIN student
ON person.ID = student.ID // example only