我試圖在調試模式下更新列中的一個單元格 ,它經歷了整個代碼,但由於某種原因,它永遠不會生效。並沒有得到任何錯誤。表格中的更新單元格不會受影響
這是我的代碼:
else//wrong try!
{
int errors;
string gameHistory = Request.QueryString[0];
errors = int.Parse(gameHistory);
string query = "UPDATE HistoryOfGames SET [email protected] WHERE ID='"+gameHistory+"'";
con = new SqlConnection("Data Source=MICROSOF-58B8A5\\SQL_SERVER_R2;Initial Catalog=Daniel;Integrated Security=True");
con.Open();
string query2 = "SELECT NumberOfErrors FROM HistoryOfGames WHERE ID='"+gameHistory+"'";
SqlCommand command = new SqlCommand(query2, con);//checks how much errors was in the last time played.
errors =(int)command.ExecuteScalar();
command = new SqlCommand(query, con);
command.Parameters.AddWithValue("@NumberOfErrors", errors);//set a new error.
command.ExecuteNonQuery();
con.Close();
}
的感謝!
你的SQL查詢讓我們遇到了你的where子句是錯的。您試圖將一個int字段與一個字符串值進行比較。一般來說,只使用SqlParameter ...有很多原因,特別是爲了避免參數類型錯誤處理(但主要原因是安全性) – 2012-01-10 10:48:03
通常ID字段需要是數字的,但在你的情況下,我看到它是一個字符串。首先,我會檢查你的字符串變量gameHistory的值。如果在那裏出了什麼問題,考慮給你的參數一個變量(代碼示例中的變量名),然後在代碼中調用它,如下所示:Request.QueryString [「VariableName」]; – 2012-01-10 10:50:16
您正在使用之前選擇的舊值更新記錄,因此難怪「永遠不會因某種原因而受到影響」。也許你想在更新之前將它增加1?! – 2012-01-10 10:51:58