I am doing some heavy lifting in an SQL database, from C#.
I keep getting the following timeout exception:
System.Data.SqlClient.SqlException:Timeout expired. The timeout period
elapsed prior to completion of the
operation or the server is not
responding.
The statement has been terminated.
I run a large number of queries (100k+), and it isn’t any one specific query that causes the exception – some times I manage to finish all of them with no exception.
Some of the exceptions come from queries run as part of a larger transaction, some of them come from queries run on their own.
When I run the same query directly in SQL Server Management Studio, they take less than a second to run.
I use the samme SQLConnection object for all the queries, but new SQLCommand object for each.
When I try using the SQL Server profiler, it just says that the batch/RPC has been completed.
Why am I getting this exception at random?
It could be due to blocking – perhaps the times when it does time out, it’s being blocked by another process that it’s waiting for to release locks. This would explain why it appears seemingly at random as it depends on what’s going on at the time.