Search code examples
t-sqlado.netsql-server-2008-express

how to try execute repeatly if command.ExecuteNonQuery() fails


how to try execute repeatly if command.ExecuteNonQuery() fails?


Solution

  • You can try

    bool executed = false;
    while (!executed)
    {
        try
        {
            command.ExecuteNonQuery();
            executed = true;
        }
        catch
        {
        }
    }
    

    You can add some more conditions like a timer or a counter but this does not seem to be a good idea. You should probably come up with a better recovery scenario.