2012-01-10 43 views
0

我試圖在調試模式下更新列中的一個單元格 ,它經歷了整個代碼,但由於某種原因,它永遠不會生效。並沒有得到任何錯誤。表格中的更新單元格不會受影響

這是我的代碼:

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(); 

} 

的感謝!

+2

你的SQL查詢讓我們遇到了你的where子句是錯的。您試圖將一個int字段與一個字符串值進行比較。一般來說,只使用SqlParameter ...有很多原因,特別是爲了避免參數類型錯誤處理(但主要原因是安全性) – 2012-01-10 10:48:03

+2

通常ID字段需要是數字的,但在你的情況下,我看到它是一個字符串。首先,我會檢查你的字符串變量gameHistory的值。如果在那裏出了什麼問題,考慮給你的參數一個變量(代碼示例中的變量名),然後在代碼中調用它,如下所示:Request.QueryString [「VariableName」]; – 2012-01-10 10:50:16

+0

您正在使用之前選擇的舊值更新記錄,因此難怪「永遠不會因某種原因而受到影響」。也許你想在更新之前將它增加1?! – 2012-01-10 10:51:58

回答

0

就像有人建議,我從來沒有改變「錯誤」整數,並沒有增加一個。

感謝所有的幫手!

1

使用SQL Server Profiler來檢查在您的服務器上執行哪個查詢。

1

我認爲你的代碼是寫的,但是語句的順序是錯誤的。

請這個,


    command = new SqlCommand(query, con);  

    command.Parameters.AddWithValue("@NumberOfErrors", errors);//set a new error.  
    command.ExecuteNonQuery();  

    SqlCommand command = new SqlCommand(query2, con);//checks how much errors was in the last time played.  

    errors =(int)command.ExecuteScalar();  
    con.Close();  

第一次更新錯誤,然後selet它。

相關問題