2012-01-10 84 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它。

相关问题