0

I'm trying to enable/disable my MySQL trigger from my C# program but allways show me the same Exception.

I also tried with SET @TRIGGER_CHECKS = FALSE on cmd.CommandText but I don't know how to do it because my program expect a parameter with that @

public void switchTriggerON()
{
  conectar();
  MySqlCommand cmd = new MySqlCommand();
  cmd.CommandText = "ALTER TRIGGER actualizacionCurso ENABLE;"; /DISABLE
  cmd.Connection = conexion;
  cmd.ExecuteNonQuery();
  conexion.Close();
}

Exception: MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TRIGGER actualizacionCurso DISABLE' at line 1 en MySql.Data.MySqlClient.MySqlStream.ReadPacket() en MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) en MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) en MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) en MySql.Data.MySqlClient.MySqlDataReader.NextResult() en MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) en MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() en ServidorConexion.Negocio.ConexionEnlaces.switchTriggerOFF() en D:\TFG\DAMnificus_Servidor\ServidorConexion\Negocio\ConexionEnlaces.cs:línea 487 en ServidorConexion.Negocio.ConexionEnlaces.cambiarCurso(String usuario, Int32 curso) en D:\TFG\DAMnificus_Servidor\ServidorConexion\Negocio\ConexionEnlaces.cs:línea 300 en ServidorConexion.Program.procesarPeticion(Peticion peticionActual, HttpListenerResponse response) en D:\TFG\DAMnificus_Servidor\ServidorConexion\Program.cs:línea 435

EDIT: WHAT I WANT IS DISABLE MY TRIGGER FROM C# CODE, NOT FROM MYSQL

  • 4
    Are you sure that this is valid MySql syntax? As far as I know there is no option to disable/enable triggers in MySql. Can you point me where you have read about this syntax on the MySql Docs? – Steve May 19 '19 at 17:23
  • I know now that code isn't valid, but I don't find any code that works – Valentín Sánchez Boto May 19 '19 at 17:31
  • You may want to try a workaround like [this](https://stackoverflow.com/q/13598155). – Solarflare May 19 '19 at 17:37
  • Possible duplicate of [How to disable triggers in MySQL?](https://stackoverflow.com/questions/13598155/how-to-disable-triggers-in-mysql) – sticky bit May 19 '19 at 17:50
  • I want to disable my trigger from C# not from MySQL – Valentín Sánchez Boto May 19 '19 at 19:10
  • To clarify: MySQL doesn't support to disable triggers (short of dropping them). You can use a workaround, like the session variable mentioned in the links, and this is possible both from MySQL and C# (by running a query). You need to modify your trigger though, basically to check inside the trigger if the trigger should run this time or not based on some condition - which can of course be something else than a session variable, e.g. the username, a column value, ... If you have specific restrictions/requirements/problems to implement this, you should specify those so we can help you with that. – Solarflare May 20 '19 at 11:54
  • I modified my database and my code to delete the trigger and execute inserts when i want, thanks btw – Valentín Sánchez Boto May 21 '19 at 07:04

0 Answers0