2016-01-18 38 views
1

我正在創建一個自動生成時間表的窗體應用程序。我有一個問題,當用戶從下拉列表中選擇學期編號時(從1-8),我應該在查詢中寫什麼?我的代碼如下。c#查詢組合框選定的文本

private void button_generate_Click(object sender, EventArgs e) 
{ 
    SqlCommand com; 
    SqlConnection con = new SqlConnection("Data Source=MAJOR-DYNASTI;Initial Catalog=ESS;Integrated Security=True"); 
    con.Open(); 
    string a = comboBox_semester.SelectedText; 
    string str = "SELECT CourseName, TeacherName, RoomName FROM Course_teacher, RoomInfo where Semester= ORDER BY NEWID(); "; 
    com = new SqlCommand(str, con); 
    SqlDataReader reader = com.ExecuteReader(); 
    if (reader.HasRows) 
    { 

     Random random = new Random(); 
     var labels = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8,label9,label10,label11,label12,label13,label14,label15,label16, 
            label17,label18,label19,label20,label21,label22,label23,label24,label25,label26,label27,label28,label29,label30,label31, 
            label32,label33,label34,label35,label36,label37,label38,label39,label40,label41,label42,label43,label44,label45,label46,label47,label48, 
            label49,label50}; 
     label1.Text = ""; label2.Text = ""; label3.Text = ""; label4.Text = ""; label5.Text = ""; label6.Text = ""; 
     label7.Text = "";label8.Text = "";label9.Text = "";label10.Text = "";label11.Text = "";label12.Text = "";label13.Text = "";label14.Text = "";label15.Text = "";label16.Text = ""; 
            label17.Text = "";label18.Text = "";label19.Text = "";label20.Text = "";label21.Text = "";label22.Text = "";label23.Text = "";label24.Text = "";label25.Text = "";label26.Text = "";label27.Text = "";label28.Text = "";label29.Text = "";label30.Text = "";label31.Text = ""; 
            label32.Text = ""; label33.Text = ""; label34.Text = ""; label35.Text = ""; label36.Text = ""; label37.Text = ""; label38.Text = ""; label39.Text = ""; label40.Text = ""; label41.Text = ""; label42.Text = ""; label43.Text = ""; label44.Text = ""; label45.Text = ""; label46.Text = ""; label47.Text = ""; label48.Text = ""; 
            label49.Text = "";label50.Text = ""; 

     if (
      comboBox1.SelectedItem==null || 
      comboBox2.SelectedItem==null || 
      comboBox_semester.SelectedItem==null) 
     { 
      MessageBox.Show("Please Select Complete Detail"); 

     } 
     else 
     { 
      while (reader.Read()) 
      { 

       int randomNumber = random.Next(1, 50); 

       labels[randomNumber].Text = String.Format("{0},\r\n{1},\r\n{2}", reader["CourseName"], reader["TeacherName"], reader["RoomName"]); 
      } 
     } 
    } 

我想爲這兩條線路查詢。

string a = comboBox_semester.SelectedText; 
string str = "SELECT CourseName, TeacherName, RoomName FROM Course_teacher, RoomInfo where Semester=(what should I need to write here) ORDER BY NEWID();); 

請幫助

+0

所以你有一個字符串持有學期選擇的用戶,你不確定如何餵它到你的查詢? –

+0

這不能回答你的問題,但是我會認爲查詢組合框選擇是不明智的,因爲用戶可以犯一個「錯誤」,或者只是隨機點擊可能會減慢/凍結你的應用程序的選項/ MVC – KGCybeX

+0

@DrewKennedy是的,這是我的問題 – Fary

回答

0

要做到這一點,最好的辦法是要看起來像這樣:

"Where Semester = @Semester" 

然後,在下面的行,你加一個參數來喜歡你的SqlCommand對象所以:

com.Parameters.AddWithValue("@Semester", a); 

這讓SqlCommand對象知道你的替代變量(我的名字d it @Semester現在)與你從用戶收到的價值。

+0

即時獲取沒有錯誤,但現在當我點擊按鈕..沒有什麼事情發生.... – Fary

+0

是您的按鈕的點擊事件設置爲您的方法,重新期待着火? – aaron

1

你需要把你的變量a到你的查詢,像這樣:

string str = "SELECT CourseName, TeacherName, RoomName FROM Course_teacher, " + 
"RoomInfo WHERE Semester = @Semester ORDER BY NEWID()"; 

com.Parameters.Add(new SqlParameter("Semester", a)); 

只要注意,我清理您的查詢一點點,我介紹了parameterized queries萬一你不熟悉他們。

+0

它向我展示了一個錯誤,說「使用UNASSIGNED LOCAL VARIABLE」com。「爲什麼它會到來? – Fary

+0

@Fary這意味着你沒有正確粘貼上面的代碼,你需要首先實例化com:'com =新的SqlCommand(str,con);' –