1

I have created a C# .NET 4.7 application that connects to several different remote SQL database instances. Sometimes connection issues may prevent the connection at a network level.

Presently I'm handling SQL timeout connections with the below code which works fine if a connection exists, but if the issue is at the network level I need to handle these, of which the timeouts are much longer.

I found an article written over 10 years ago here http://improve.dk/controlling-sqlconnection-timeouts/. I was wondering if anyone could recommend whether this is current best practice or whether there is a better solution?

    if ( e is SqlException f)
            {
                if (f.Number == -2 | f.Number == 258)
                {  do stuff} 
            }
10
  • The connection should be instant unless the server is chained, or there is a proxy and the proxy timed out. Commented Mar 2, 2020 at 15:57
  • some of the servers are remote via vpn so I need to handle whether the connection is available or whether someone has switched the computer off!. Commented Mar 2, 2020 at 15:58
  • Lengthening the timeout is not going to fix a problem where the server is turned off. It is just going to prolong the time it takes to discover that the server is turned off. The default timeout is 30 seconds and it would be better to shorten the timeout rather than have somebody wait 30 seconds for the error. You probably want to add an exception handler to capture the timeout and then determine what to do next. Commented Mar 2, 2020 at 16:14
  • When the server is turned off though the sql timeout doesn't seem to be triggered at 30 seconds runs what seems like indefinitely so I figure it was probably a network level timeout TCP which is 15minutes? Commented Mar 2, 2020 at 16:25
  • should I test connection first ? something like this stackoverflow.com/questions/136615/… Commented Mar 2, 2020 at 16:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.