I was talking to one of the guys at work about Sql timeouts. He had a long running operation – about 40 seconds – so to compensate for this, he set the timeout on his connection string to one minute:
<connectionStrings>
<add name="cs"
connectionString="Data Source=localhost;Initial Catalog=MyDB;Connection Timeout=60;"
</connectionStrings>
Sure looks OK, but as it turns out, this wasn’t working for him. The reason is actually quite straightforward – the Connection Timeout property is used to specify how long the application will wait to establish a connection to the server before giving up and throwing an exception. In most cases (where connections are properly managed), the connection will be returned almost instantaneously, hence changing this value is usually not required.
Instead, the timeout that he needed to change was the CommandTimeout on the SqlCommand object which was being passed to the database.
SqlCommand command = new SqlCommand();
command.CommandTimeout = 60;
As the MSDN doco states, this property "Gets or sets the wait time before terminating the attempt to execute a command and generating an error". And sure enough, once he changed this value to 60, everything was dandy.
December 23rd, 2006 at 12:38 am #ben
I was a young player a few months back, and discovered this exact same situation.
Nice one!