2013-09-16 66 views
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; 
} 

和按鈕:爲什麼我的SELECT與dataGrid重複?

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

爲什麼會變成這樣給我這樣的按鈕,結果點擊?因爲你沒有定義的記錄如何相互關聯

enter image description here

+2

可能是因爲連接條件(或缺乏) – Ric

回答

6

你查詢產生笛卡爾乘積。您需要添加一個條件,

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

這意味着,從表person記錄與IDstudent有關。

爲了進一步獲得更多的知識有關加入,請訪問以下鏈接:

+0

謝謝你,這個工作。在10分鐘內給出正確的答案。 – user2469932

相關問題