.net SqlCommand.CommandTimeout 和 SqlConnection.ConnectionTimeout 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/847264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:40:43  来源:igfitidea点击:

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

.netsqlconnectionsqlcommand

提问by Dhana

Is there any difference between SqlCommand.CommandTimeoutand SqlConnection.ConnectionTimeoutin .NET?

.NETSqlCommand.CommandTimeoutSqlConnection.ConnectionTimeout.NET之间有什么区别吗?

回答by Jon Skeet

Yes. CommandTimeoutis how long a single command can take to complete. ConnectionTimeoutis how long it can take to establish a connection to the server to start with.

是的。CommandTimeout是单个命令完成所需的时间。ConnectionTimeout是开始与服务器建立连接需要多长时间。

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.

例如,您可能正在执行运行时间相对较长的查询——它们花 10 分钟完成是完全可以的,但如果开始连接需要 10 分钟,您就会知道某些事情严重错误。

回答by NinethSense

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.

SqlCommand.CommandTimeout= SQL 查询的超时限制。意思是,执行(例如:SELECT、UPDATE)查询需要多长时间。如果超过 SqlCommand.CommandTimeout,则停止执行。将发生命令超时错误。

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.

SqlConnection.ConnectionTimeout= 连接的超时限制。意思是,您的连接对象可以尝试连接多少时间。如果超过指定时间,则停止连接。将发生连接超时错误。

回答by Cerebrus

ConnectionTimeoutspecifies the duration to wait before timing out when attempting to openan SqlConnection. It is relevant to the Connection.Open()command.

ConnectionTimeout指定时间到时间之前等待试图外出时打开SqlConnection。它与Connection.Open()命令有关。

while

尽管

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

SqlCommand.CommandTimeout指定 SqlCommand 在超时之前等待的持续时间。这发生在打开连接并且ExecuteXXX在 Command 对象上调用了其中一种方法之后。

回答by Htin Aung

Additional Info

附加信息

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

默认值为CommandTimeout30 秒。零(0) 表示没有限制。您只能CommandTimeout在编码中设置值。

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

默认值为ConnectiontTimeout15 秒。零(0)也表示没有限制。小于零值(负值)将得到ArgumentException. 您可以ConnectionTimeout在编码和配置文件中设置值。

回答by dodng

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