how to try execute repeatly if command.ExecuteNonQuery() fails
-
21-09-2019 - |
Question
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.
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow