Can I increase the timeout by modifying the connection string in the web.config
?

- 17,291
- 7
- 48
- 81

- 9,069
- 14
- 70
- 129
-
7Don't read this like I did thinking the timeout would control query execution--it does not, only the timeout to connect to the server--"connecting timeout" would be a better name. See http://stackoverflow.com/a/7976867/409856 – downwitch May 30 '14 at 19:07
3 Answers
Yes, you could append ;Connection Timeout=30
to your connection string and specify the value you wish.
The timeout value set in the Connection Timeout
property is a time expressed in seconds. If this property isn't set, the timeout value for the connection is the default value (15 seconds).
Moreover, setting the timeout value to 0
, you are specifying that your attempt to connect waits an infinite time. As described in the documentation, this is something that you shouldn't set in your connection string:
A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.

- 7,972
- 3
- 38
- 61

- 1,023,142
- 271
- 3,287
- 2,928
-
Can you give example for this? I'm using EF6 an i have 3 million data in my table when i do some process program returns timeout error... note:i'm using indexing in table – saulyasar Nov 13 '15 at 12:26
-
5@saulyasar This is the CONNECTION time out, not the COMMAND timeout. CONNECTION timeout is about how long it will try to CONNECT to sql server. You want to investigate the COMMAND timeout, which is how long a procedure or statement will run before timing out. – granadaCoder Dec 15 '16 at 15:54
Hmmm...
As Darin said, you can specify a higher connection timeout value, but I doubt that's really the issue.
When you get connection timeouts, it's typically a problem with one of the following:
Network configuration - slow connection between your web server/dev box and the SQL server. Increasing the timeout may correct this, but it'd be wise to investigate the underlying problem.
Connection string. I've seen issues where an incorrect username/password will, for some reason, give a timeout error instead of a real error indicating "access denied." This shouldn't happen, but such is life.
Connection String 2: If you're specifying the name of the server incorrectly, or incompletely (for instance,
mysqlserver
instead ofmysqlserver.webdomain.com
), you'll get a timeout. Can you ping the server using the server name exactly as specified in the connection string from the command line?Connection string 3 : If the server name is in your DNS (or hosts file), but the pointing to an incorrect or inaccessible IP, you'll get a timeout rather than a machine-not-found-ish error.
The query you're calling is timing out. It can look like the connection to the server is the problem, but, depending on how your app is structured, you could be making it all the way to the stage where your query is executing before the timeout occurs.
Connection leaks. How many processes are running? How many open connections? I'm not sure if raw ADO.NET performs connection pooling, automatically closes connections when necessary ala Enterprise Library, or where all that is configured. This is probably a red herring. When working with WCF and web services, though, I've had issues with unclosed connections causing timeouts and other unpredictable behavior.
Things to try:
Do you get a timeout when connecting to the server with SQL Management Studio? If so, network config is likely the problem. If you do not see a problem when connecting with Management Studio, the problem will be in your app, not with the server.
Run SQL Profiler, and see what's actually going across the wire. You should be able to tell if you're really connecting, or if a query is the problem.
Run your query in Management Studio, and see how long it takes.
Good luck!

- 28,657
- 18
- 88
- 151
-
if your query is timing out then i guess you will get a commandtimeout – user55474 Nov 29 '10 at 22:01
-
@user55474 probably; depends (**very** rarely) on how it's being called. – 3Dave Nov 29 '10 at 22:06
-
Not according to this msdn blog post: http://blogs.msdn.com/b/spike/archive/2008/07/31/timeout-expired-the-timeout-period-elapsed-prior-to-completion-of-the-operation-or-the-server-is-not-responding.aspx – tom redfern Sep 10 '12 at 13:20
-
-
Sorry I misread - thought you were saying the commandtimeout was a very rare cause, but re-reading your comment can see you didn't – tom redfern Sep 10 '12 at 15:38
-
1+1 for "Run SQL Profiler". It sounds like it will be complicated but you actually just click "Tools > SQL Server Profiler" and watch what your app is saying to SQL Server. It turns out mine was Option #5 :) – Andrew Kvochick Aug 29 '15 at 20:07
If you want to dynamically change it, I prefer using SqlConnectionStringBuilder .
It allows you to convert ConnectionString i.e. a string into class Object, All the connection string properties will become its Member.
In this case the real advantage would be that you don't have to worry about If the ConnectionTimeout string part is already exists in the connection string or not?
Also as it creates an Object and its always good to assign value in object rather than manipulating string.
Here is the code sample:
var sscsb = new SqlConnectionStringBuilder(_dbFactory.Database.ConnectionString);
sscsb.ConnectTimeout = 30;
var conn = new SqlConnection(sscsb.ConnectionString);

- 7,331
- 11
- 57
- 101
-
2I can't believe that making the `ConnectionTimeout` property on the `SqlConnection` type read-only was thought to be a good idea. – Maslow Oct 26 '17 at 14:22
-
1IMHO, it is a good idea to make ConnectionTimeout "immutable". https://www.codeguru.com/csharp/c-sharp-immutability/. I cannot even imagine the amount of hacked up code that would "change the connection timeout here, there, here, other-there" and confusing it with the Command-Timeout. – granadaCoder May 02 '23 at 11:48