2017-08-10 100 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(& affectedRowsのInt32 STATEMENTID、のInt32、Int64の& insertedId) 在MySql.Data.MySqlClient.Driver.NextResult( 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

ヘルプ

号行

please

+0

@fubo:彼らは数字ではなく、テキストです。 –

+4

最初にすること:値をSQLに直接埋め込むことをやめてください。代わりにパラメータ化されたSQLを使用してください。たとえ整数に害を及ぼさないと思っても、それは習慣に入る価値があります。 (Heck [偶数は本当に安全ではありません](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); 
...