Catching A Db Timeout Exception

  • Thread starter Thread starter Garrek
  • Start date Start date
G

Garrek

Is there a specific exception thrown for timeouts occurring from a
database call? If not, does anyone have a recommendation on how I can
write a loop to run say three times if a timeout does occur.

Thanks
 
There are a few possible timeouts that you can be dealing with
1) CommandTimeout
2) Connection Timeout (Read Only - except it can be set in the connection
string)
3) Transaction Timeout
4) ASP.NET Timeout (If that's what you are dealing with).

For instance, if you are in ASP.NET and your command takes longer to process
than ASP.NET's timeout, it will be cancelled. The rest are pretty much what
you'd expect them to be.
You can adjust the timeout time to fit your task. However, that's often a
band aid so I'd first look into why I'm getting the timeout and try to solve
it that way.


YOu can use OleDbException or SqlException (or whatever provider) and trap
the exception from there

using(SqlConnection cn = new SqlConnection("ConnectString"){
try{
cn..Open();
}
catch(SqlException ex){
Debug.Assert(false, ex.ToSTring();{
}
}

you can do the same with cmd.ExecuteNonQuery, ExecuteReader etc or
dataAdapter.Fill.. Also, there's an errors collection in SQLException so
you'll probably want to walk through it - my example up there is a bit lame.

HTH,

Bill

--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
Back
Top