2012-12-20 99 views
-2

我有3個不同的SQL代碼。我把我的3個文本(男人,女人,兩個)都放在組合框中。我不知道如何在通過示例選擇組合框中的Men時,使用好的SQL代碼。ComboBox/SelectedIndexChanged with SQL

戰警:

SELECT EmployeeID, FirstName, LastName, MaritalStatus, Gender 
FROM Employes, Contacts 
WHERE Employes.ContactId = Contacts.ContactID 
    AND Gender = 'M' 
ORDER BY LastName, FirstName 

女人:

SELECT EmployeeID, FirstName, LastName, MaritalStatus, Gender 
FROM Employes, Contacts 
WHERE Employes.ContactId = Contacts.ContactID 
    AND Gender = 'F' 
ORDER BY LastName, FirstName 

兩個:

SELECT EmployeeID, FirstName, LastName, MaritalStatus, Gender 
FROM Employes, Contacts 
WHERE Employes.ContactId = Contacts.ContactID 
ORDER BY LastName, FirstName 

看(在法國)(Choisir樂sexe =選擇sexe)(Afficher =展英文)

enter image description here

非常感謝!

+1

你需要寫在組合框中創建選擇(使用新的System.EventHandler(ComboBox1_SelectedIndexChanged);)的組件時執行C#代碼,然後運行相應的SQL調用。 – scrappedcola

+2

[踢壞的習慣:使用舊式JOIN](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins。 aspx) - 使用ANSI - ** 92 ** SQL標準(** 20年前**!)廢止舊式*逗號分隔的表*樣式列表。 ***請***停止使用它 –

回答

1

假如你填寫你的組合框用三根弦Hommes, Femmes, Tous那麼當您收到事件SelectedIndexChanged你可以寫這樣的事情

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    // Get the currently selected item in the ListBox. 
    string curItem = comboBox1.SelectedItem.ToString(); 
    string sqlText = "SELECT EmployeeID, FirstName, LastName, MaritalStatus, Gender " + 
        "FROM Employes JOIN Contacts ON Employes.ContactId = Contacts.ContactID " 
    switch(curItem) 
    { 
     case "Femmes": 
      sqlText += "AND Gender = 'F' ORDER BY LastName, FirstName"; 
      break; 
     case "Hommes": 
      sqlText += "AND Gender = 'M' ORDER BY LastName, FirstName"; 
      break; 
     default: 
      sqlText += "ORDER BY LastName, FirstName"; 
    } 
    DataTable dt = GetEmployeeList(sqlText); 
    ...... // Now fill the datasource of your grid 
} 

編輯:嗯,剛看完@marc_s評論

+0

非常感謝男人! +聲望!和平! – Seadow11