我有5個文本框,我想遍歷它們並獲取每個文本框的值並將其插入到一個單獨的行中的表中。我怎樣才能做到這一點?我想是這樣的:循環通過文本框c#
TextBox[] rasp = new TextBox[] { textbox1, textBox2, textBox3,
textBox4, textBox5 };
foreach (TextBox raspuns in rasp) {
SqlConnection con = new SqlConnection(
ConfigurationManager.ConnectionStrings["chestionar"].ConnectionString);
SqlCommand cmd = new SqlCommand(
"INSERT INTO Raspunsuri Values('" + raspuns +"',@cnp,@data,'5')", con);
cmd.Parameters.AddWithValue("@cnp", Session["sesiune_cnp"]);
//cmd.Parameters.AddWithValue("@raspuns", raspuns);
cmd.Parameters.AddWithValue("@data", DateTime.Now.ToLocalTime());
}
您應該將SQLConnection代碼移到循環外部 – 2012-01-27 14:06:50
您可以使用George的解決方案,但我可以建議您將SqlConnection代碼移到循環外部。 – Raj 2012-01-27 14:07:49
我喜歡你所控制的兩個值是如何參數化的,並且容易被注入的值在查詢 – John 2012-01-27 14:10:19