0

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

loviji
  • 12,620
  • 17
  • 63
  • 94
  • 1
    It would help to know what you mean by failure. A timeout perhaps? Are you looking for a specific exception to be thrown? – Andy West Nov 24 '09 at 08:31

2 Answers2

3

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.

Serhat Ozgel
  • 23,496
  • 29
  • 102
  • 138
1

The simplest way I can think is:

while(true) {
    try {
        command.ExecuteNonQuery();
        break;
    } catch(SqlException ex) { }
}   

You should anyway put some extra control code in the catch block to prevent an infinite loop and/or to log the error.

Konamiman
  • 49,681
  • 17
  • 108
  • 138