Sometimes you want to catch and specially handle an SQL-Timeout.
To do this, just put a try-catch around your SQL-Call and catch the SqlException. Now check the Property Number if it is -2 (which stands for Timeout). That’s it.

Here’s a little Code Example how that could look like:

try
{
	RunSomeSqlQuery();
}
catch (SqlException ex)
{
	// Check if it's a TimeoutException
	if (ex.Number == -2)
	{
		// It is...
		// Do whatever you need
	}
	else
	{
		// Other Type of Exception, re-throw it or whatever
		throw;
	}
}

If you want to simulate a Timeout in your SQL-Statement, just use the WAITFOR DELAY SQL-Statement to block for some Time and therefore results in a Timeout.

Here’s an example to wait for 1 Minute:

WAITFOR DELAY '00:01:00'

2 Comments

  1. Gail Bowen

    Thank you – this was very helpful.

  2. Luis

    Thank you, this was helpful.

Leave a Reply

Your email address will not be published.

*