Search code examples
c#foreachpetapoco

How to know if every queries executed successfully in loop?


Suppose I have executed an update query in loop using PetaPoco like,

foreach (var obj in mainObject) {
   db.Execute("update Table set Column = @0 where C1=@1 and C2 =@2", Column1, obj.data1, obj.data2);
}

How to know if each of these queries has been executed successfully ?


Solution

  • Usually PetaPoco returns 1 OR greater if a single query is executed successfully or means if any rows are affected and 0 if failed.

    With this scenario you can trace those values by adding that in the loop, like:

    List<int> checkSuccess = new List<int>(); //To trace the value returned by execute query
    
    foreach (var obj in mainObject) {
        int updated = db.Execute("update Table set Column = @0 where C1=@1 and C2 =@2", Column1, obj.data1, obj.data2);
        checkSuccess.Add(updated);
    }
    
    if (checkSuccess.All(i => i >= 1))
    {
        //Your every queries has been updated successfully
    }