取代問題:Update multiple rows into SQL table多個數據庫更新:
下面是更新考試結果集的代碼片段。 DB結構是給定的,但我可以提交包含存儲過程(這是修改痛苦,所以我保存到最後。)
的問題:有沒有使用SQL v服務器2005年有更好的方式。 ,淨2.0?
string update = @"UPDATE dbo.STUDENTAnswers
SET [email protected]
WHERE StudentID [email protected] and QuestionNum [email protected]";
SqlCommand updateCommand = new SqlCommand(update, conn);
conn.Open();
string uid = Session["uid"].ToString();
for (int i= tempStart; i <= tempEnd; i++)
{
updateCommand.Parameters.Clear();
updateCommand.Parameters.AddWithValue("@ID",uid);
updateCommand.Parameters.AddWithValue("@qnum",i);
updateCommand.Parameters.AddWithValue("@answer", Request.Form[i.ToString()]);
try
{
updateCommand.ExecuteNonQuery();
}
catch { }
}
每次回發循環運行約30次。 我的想法是,我可以節省30次開閉。我錯了嗎? – 2008-10-08 19:49:16
我同意,我寧願有一個打開的連接,無論循環需要執行多長時間,都要打開和關閉連接N次。 – palmsey 2008-10-08 20:45:56