取代问题: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