views:

4229

answers:

4

Is there any difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout in .NET?

+12  A: 

Yes. CommandTimeout is how long a single command can take to complete. ConnectionTimeout is how long it can take to establish a connection to the server to start with.

For instance, you may be executing relatively long-running queries - it's perfectly okay for them to take 10 minutes to complete, but if it took 10 minutes to make the connection to start with, you'd know that something was badly wrong.

Jon Skeet
Out of curiosity:Since the default timeout is 30 seconds, what happens if I set CommandTimeout to 60 seconds but do not change ConnectionTimeout from the default?
flipdoubt
What do you mean? Either you're setting CommandTimeout or you're not...
Jon Skeet
For the sake of argument, let's say I have a !**Q@? query that takes 32 seconds to run. If I set SqlCommand.CommandTimeout = 40 but leave SqlConnection.ConnectionTimeout at its default (presumably 30), will the connection timeout? In other words, do I have to set both properties? It sounds like you are saying "no," but I must have forgotten about the SqlConnection.ConnectionTimeout property and started questioning whether setting CommandTimeout does everything I need it to.
flipdoubt
flipdoubt - the CommandTimeout will affect the query, the ConnectionTimout won't. The ConnectionTimout isn't a timeout for the connection to peform queries - it's just the timeout for the connection to connect to the database in the first place.
Robin Bennett
+5  A: 

SqlCommand.CommandTimeout = timeout limit for your SQL query. Means, how much time a (eg: SELECT, UPDATE) query can take for its execution. If it exceeds SqlCommand.CommandTimeout, then it stops execution. A command timeout error will occur.

SqlConnection.ConnectionTimeout = timeout limit for your connection. Means, how much time your connection object can try to connect. If it exceeds the specified time, it stops connecting. A connection timeout error will occur.

NinethSense
+2  A: 

ConnectionTimeout specifies the duration to wait before timing out when attempting to open an SqlConnection. It is relevant to the Connection.Open() command.

while

SqlCommand.CommandTimeout specified the duration for an SqlCommand to wait before timing out. This happens after a connection has been opened and one of the ExecuteXXX methods have been called on the Command object.

Cerebrus
A: 

Does anyone know the TSQL to increase the command timeout?

Jim
The command timeout is set on the client side, since that's what's doing the waiting. So there isn't a TSQL way to set it. If you're wanting to set it within SQL Management Studio, it's under Tools->Options->Query Execution->SQL Server->General->Execution time-out.
Dewayne Christensen