SqlCommand对象,CommandTimeout的时间长度是多少?

发布于 2024-11-29 04:41:51 字数 198 浏览 4 评论 0原文

使用 SqlCommand 对象时如何决定使用多长的时间作为超时?

关于我正在处理的部分代码(由其他人编写),我有:

cmd.CommandTimeout = 60;

我认为这很短。然而,我在论坛上看到有人谈论将其设置为30000,这似乎太长了。

我如何知道什么最适合我的应用程序?

How do I decide what length of time to use as a timeout when using an SqlCommand object?

On parts of the code I'm working on (written by somebody else) I have:

cmd.CommandTimeout = 60;

Which I think is quite short. However, I have seen some people in forums talking about setting it to 30000, which seems too long.

How do I know what is best for my application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

携余温的黄昏 2024-12-06 04:41:51

人们似乎很困惑这是秒还是毫秒。 文档指出超时是几秒钟内。对于大多数查询来说,1 分钟的超时似乎是合理的。 8 小时以上的超时似乎不合理。

您是否有预计需要超过一分钟的查询?如果是这样,请将其提高到您期望的值,该值高于一切正常工作时您会看到的任何值,但不要太高,以免永远提醒您出现问题。 (例如,您可以从 1 分钟延长到 5 分钟。)

It seems that people are confused as to whether this is seconds or milliseconds. The documentation states that the timeout is in seconds. A 1-minute timeout seems reasonable for most queries. An 8+ hour timeout doesn't seem reasonable.

Do you have queries you're expecting to take longer than a minute? If so, raise it to a value you expect to be higher than anything you'd see if everything is working properly, but not so high as to take forever to alert you to a problem. (For example, you might go from 1 minute to 5 minutes.)

海拔太高太耀眼 2024-12-06 04:41:51

默认值为 30 秒 (http:// msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx)。

如果您的数据库位于同一台计算机上,通常无需更改它。
只需尝试一下:如果您遇到连接超时,请增加该值。

The default value is 30 seconds (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx).

If your database is on the same machine you usually don't have to change it.
Just try it out: if you experience connection timeouts increase the value.

葬﹪忆之殇 2024-12-06 04:41:51

您应该计算最长的查询需要多长时间,并可能将该值乘以 1.5 以处理服务器过载、网络流量等...

You should calculate how much time will take the longest possible query and probably multiply that value by 1.5 to handle server overload, network traffic, etc ...

把人绕傻吧 2024-12-06 04:41:51

这是以秒为单位的,完全取决于实际的 SQL 命令可能需要多长时间。

例如,我们的大多数命令运行的存储过程应该不超过 30 秒才能完成,但是有一些命令运行时间更长,这意味着它们有自己的高超时值。

您需要分析您的日常工作平均需要多长时间,然后相应地调整超时,并记住为延迟等变量留出空间。

This is in SECONDS and it depends entirely on how long the actual SQL command is likely to take.

For example example, most of our commands run sproc's that should take no longer than 30 seconds to complete, however there are a couple that run for much longer, which means they have their own high valued timeouts.

You'll need to profile how long on average your routine takes then adjust the timeout accordingly, and remember to leave room for variables like latency etc.

再浓的妆也掩不了殇 2024-12-06 04:41:51

SqlCommand.CommandTimeout 用于设置终止尝试执行命令之前的等待时间,这意味着您希望给 SqlCommand 执行特定 sql 查询或存储过程的时间并等待它完成。如果命令没有在指定时间内完成,它将被终止并抛出异常。

因此,设置CommandTimeout的值完全取决于您的应用程序和要求,如果您知道sql语句或您正在执行的存储过程只需将其设置为大于 60 的值即可,否则 60 秒似乎足以等待任何语句的完成。

SqlCommand.CommandTimeout is used to set waiting time before terminating the attempt to execute a command, which means that the time you wanna give your SqlCommand to execute particular sql query or stored procedure and wait for it to complete. If command doesn't completed in specified time than it will be terminated and exception will be thrown..

So, setting the value of CommandTimeout totally depends on your application and requirements, if you know the sql statement or stored procedure you are executing will take little than set it to value greater than 60, otherwise 60 seconds seems to be enough to wait for completion of any statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文