Skip to main content
1 de 2
MCcover
  • 151
  • 1
  • 10

C# Error al ejecutar update con with

Tengo la siguente consulta:

WITH lectura AS (
    SELECT * FROM antenas_registros 
    WHERE tag = 'tag' 
    ORDER BY ID DESC 
    LIMIT 1
),
actualiza AS (
    SELECT 
        IIF((SELECT COUNT(*) FROM lectura) > 0, 
                (('2021-08-11 11:18:13' - (SELECT salida FROM lectura)) < '00:03:00'), 
                FALSE) AS actu
)
UPDATE antenas_registros 
SET salida = IIF((SELECT actu FROM actualiza LIMIT 1), '2021-08-11 11:18:13'::TIMESTAMP, (SELECT salida FROM lectura)::TIMESTAMP)
WHERE tag = 'tag' AND
         salida = (SELECT salida FROM lectura)::TIMESTAMP

El ejecutar la consulta con navicat funciona correctamente, pero al correrla desde mi programa en c# me dispara el error:

Operation is not allowed when the object is closed

la conexion esta abierta para la instancia de ejecicion de esta Query

public static bool Modificar(Registro obj, out int recodsAffecteds, double difEnMinutos = 3) {
        string fechaSalida = obj.Salida.ToString("yyyy-MM-dd HH:mm:ss").InsertStringInSQL();
        string tag = obj.Tag.InsertStringInSQL();

        TimeSpan t = TimeSpan.FromMinutes(difEnMinutos);
        string diferencia = (((t.Hours <= 9) ? "0" : "") + t.Hours + ":" + ((t.Minutes <= 9) ? "0" : "") + t.Minutes + ":" + ((t.Seconds <= 9) ? "0" : "") + t.Seconds).InsertStringInSQL();
        Command cmd = new Command {
            ActiveConnection = _Connection,
            CommandText = "WITH lectura AS (\n" +
                          " SELECT * FROM antenas_registros \n" +
                         $" WHERE tag = {tag} \n" +
                          " ORDER BY ID DESC \n" +
                          " LIMIT 1\n" +
                          "),\n" +
                          "actualiza AS (\n" +
                          " SELECT \n" +
                          "     IIF((SELECT COUNT(*) FROM lectura) > 0, \n" +
                         $"             (({fechaSalida} - (SELECT salida FROM lectura)) < {diferencia}), \n" +
                          "             FALSE) AS actu\n" +
                          ")\n" +
                          "UPDATE antenas_registros \n" +
                         $"SET salida = IIF((SELECT actu FROM actualiza LIMIT 1), {fechaSalida}::TIMESTAMP, (SELECT salida FROM lectura)::TIMESTAMP)\n" +
                         $"WHERE tag = {tag} AND\n" +
                          "      salida = (SELECT salida FROM lectura)::TIMESTAMP"
        };
        recodsAffecteds = cmd.ExecuteNonQueryCount();
        return recodsAffecteds > 0;
    }

este es el codigo que ejecuto para realizar la consulta

MCcover
  • 151
  • 1
  • 10