2017-08-10 221 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); 
...