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

一世执手 提交于 2019-11-26 22:05:23

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.

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.

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.

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.

dodng
select @@LOCK_TIMEOUT //get the TIMEOUT,default is -1
set LOCK_TIMEOUT = 600//set TIMEOUT with ms
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!