我正在寫asp.net中的代碼..我需要從數據庫打印問題到標籤和選項從相同的數據庫表到Radiobutton。我在下面的代碼中使用了sqldataReader,但問題是它只打印一個來自數據庫的問題,而沒有在下一個標籤中輸出下一個問題。使用SqldataReader從數據庫檢索數據到標籤
connection.Open();
SqlCommand cmd = new SqlCommand("Select * from Questable1 ", connection);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
Label1.Text = reader["Ques"].ToString();
RadioButton1.Text = reader["Option1"].ToString();
RadioButton2.Text = reader["Option2"].ToString();
RadioButton3.Text = reader["Option3"].ToString();
RadioButton4.Text = reader["Option4"].ToString();
Label2.Text = reader["Ques"].ToString();
RadioButton5.Text = reader["Option1"].ToString();
RadioButton6.Text = reader["Option2"].ToString();
RadioButton7.Text = reader["Option3"].ToString();
RadioButton8.Text = reader["Option4"].ToString();
connection.Close();
}
}
}
}
你的邏輯看起來很混亂。所以你已經知道表中的問題數量了嗎?他們會一直相同嗎?您將不得不爲所有問題手動添加標籤。我更喜歡在這種情況下使用gridview。另外,你並沒有圍繞閱讀器循環,以便whoe表得到顯示。 – RelatedRhymes