What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout?

.NetSqlconnectionSqlcommand

.Net Problem Overview


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

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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.

Solution 3 - .Net

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.

Solution 4 - .Net

Additional Info

Default value of CommandTimeout is 30 seconds. Zero(0) indicates no limit. You can set CommandTimeout value in Coding only.

Default value of ConnectiontTimeout is 15 seconds. Zero(0) indicates no limit as well. Less than zero value (minus value) will get ArgumentException. You can set ConnectionTimeout value in both Coding and Config file.

Solution 5 - .Net

select @@LOCK_TIMEOUT //get the TIMEOUT,default is -1
set LOCK_TIMEOUT = 600//set TIMEOUT with ms

Solution 6 - .Net

Quick note regarding CommandTimeout, since it is a property of both Connection and Command objects...

The CommandTimeout setting on a Connection object has no effect on the CommandTimeout setting on a Command object on the same Connection; that is, the Command object's CommandTimeout property does not inherit the value of the Connection object's CommandTimeout value.

So the CommandTimeout setting on a Connection object only affects commands executed under the Connection object only (without using a Command object).

e.g. When you're connecting to a Stored Proc and adding parameters to the command object, and executing the Command object using a Connection Object's connection, then you would need to set CommandTimeout on the Command object and the ConnectionTimeout on the Connection object to override both defaults. Setting the CommandTimeout on the Connection Object will not override the default timeout for the Command Object commands.

https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/commandtimeout-property-ado?view=sql-server-ver15 https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/connectiontimeout-property-ado?view=sql-server-ver15

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDhanapalView Question on Stackoverflow
Solution 1 - .NetJon SkeetView Answer on Stackoverflow
Solution 2 - .NetNinethSenseView Answer on Stackoverflow
Solution 3 - .NetCerebrusView Answer on Stackoverflow
Solution 4 - .NetHtin AungView Answer on Stackoverflow
Solution 5 - .NetdodngView Answer on Stackoverflow
Solution 6 - .NetBrad SkidmoreView Answer on Stackoverflow