我正在創建一個自動生成時間表的窗體應用程序。我有一個問題,當用戶從下拉列表中選擇學期編號時(從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(););
請幫助
所以你有一個字符串持有學期選擇的用戶,你不確定如何餵它到你的查詢? –
這不能回答你的問題,但是我會認爲查詢組合框選擇是不明智的,因爲用戶可以犯一個「錯誤」,或者只是隨機點擊可能會減慢/凍結你的應用程序的選項/ MVC – KGCybeX
@DrewKennedy是的,這是我的問題 – Fary