2017-08-10 218 views
1

我的代码中有错误偶尔(windows服务)。我用的断点,但没有got.Who可以帮我C#MySql 0x80004005):SQL语法错误;

private void SetGPSIsLogOut(int shopId,int status) 
    { 
     MySqlConnection conn = ConnectDataBase(); 
     strcmd.Clear(); 
     strcmd.AppendLine("update multi_shop_gps_location_log set gps_is_logout="+status+" where shop_id=" + shopId + " "); 
     MySqlCommand myCommand = new MySqlCommand(strcmd.ToString()); 
     myCommand.Connection = conn; 
     try 
     { 
      conn.Open(); 
      myCommand.ExecuteNonQuery(); 
      conn.Close(); 
     } 
     catch (MySqlException e) 
     { 
      UpdateServicestatus(-1); 
      WriteLog("SetGPSIsLogOut 更新失败" + e); 
     } 
    } 

例外:

MySql.Data.MySqlClient.MySqlException(0X80004005):你在你的SQL语法错误;检查与您的MariaDB服务器版本相对应的手册,以获取在第2行使用附近'update multi_shop_gps_location_log set gps_is_logout = 0 where shop_id = 513'时使用的正确语法 在MySql.Data.MySqlClient.MySqlStream.ReadPacket() 在MySql.Data .MySqlClient.NativeDriver.GetResult(的Int32 & affectedRow,Int64的& insertedId) 在MySql.Data.MySqlClient.Driver.GetResult(的Int32 statementId,的Int32 & affectedRows,Int64的& insertedId) 在MySql.Data.MySqlClient.Driver.NextResult( Int32 statementId,布尔力) 在MySql.Data.MySqlClient.MySqlDataReader.NextResult() 在MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior行为) 在MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() 在ys_service.Service1.SetGPSIsLogOut(的Int32 shopId,的Int32状态)位置E:\服务\服务\ Service1.cs:行号645

帮助

+0

@fubo:他们是数字,而不是文字。 –

+4

要做的第一件事:停止将值直接嵌入到SQL中。改用参数化的SQL。即使你认为它不会伤害整数,也值得养成这种习惯。 (嘿[偶数不是很安全](https://codeblog.jonskeet.uk/2014/08/08/the-bobbytables-culture/)。) –

+0

尝试使用字符串变量代替StringBuilder'strcmd' –

回答

2

错误是由于您没有在连接的查询字符串上使用引号而引起的。

然而,正确的方法是使用参数,而不是字符串连接:

... 
string query ="update multi_shop_gps_location_log set [email protected] where [email protected]"; 
MySqlCommand myCommand = new MySqlCommand(query); 
myCommand.Parameters.Add("@status",status); 
myCommand.Parameters.Add("@shopId ",shopId); 
...