SQLcommandTimeout = -1 是什么意思?
What does SQLcommandTimeout = -1 mean?
最近看到一些项目设置了这个值,想知道到底是什么意思,默认还是最大允许?
如果超时设置的值小于1
,它会将超时设置为无穷大:它会一直等待,直到命令执行或失败。
我猜想不会超时或无穷大,但推荐的方法是将值设置为 0,如此处所示 SqlCommand.CommandTimeout
可能不知道将值设置为 0 就可以完成工作。
使用 -1 的一个可能原因是作者想要一个没有超时的命令。但是,应更改代码以将超时设置为零,这意味着 "no timeout" 根据 documentation:
A value of 0 indicates no limit (an attempt to execute a command will wait indefinitely).
根据具体实施情况,为 CommandTimeout
设置负值可能会引发异常。这是来自 Microsoft Reference Source code:
的示例
override public int CommandTimeout { // V1.2.3300, XXXCommand V1.0.5000
get {
return _commandTimeout;
}
set {
Bid.Trace("<sc.SqlCommand.set_CommandTimeout|API> %d#, %d\n", ObjectID, value);
if (value < 0) {
throw ADP.InvalidCommandTimeout(value);
}
if (value != _commandTimeout) {
PropertyChanging();
_commandTimeout = value;
}
}
}
最近看到一些项目设置了这个值,想知道到底是什么意思,默认还是最大允许?
如果超时设置的值小于1
,它会将超时设置为无穷大:它会一直等待,直到命令执行或失败。
我猜想不会超时或无穷大,但推荐的方法是将值设置为 0,如此处所示 SqlCommand.CommandTimeout
可能不知道将值设置为 0 就可以完成工作。
使用 -1 的一个可能原因是作者想要一个没有超时的命令。但是,应更改代码以将超时设置为零,这意味着 "no timeout" 根据 documentation:
A value of 0 indicates no limit (an attempt to execute a command will wait indefinitely).
根据具体实施情况,为 CommandTimeout
设置负值可能会引发异常。这是来自 Microsoft Reference Source code:
override public int CommandTimeout { // V1.2.3300, XXXCommand V1.0.5000
get {
return _commandTimeout;
}
set {
Bid.Trace("<sc.SqlCommand.set_CommandTimeout|API> %d#, %d\n", ObjectID, value);
if (value < 0) {
throw ADP.InvalidCommandTimeout(value);
}
if (value != _commandTimeout) {
PropertyChanging();
_commandTimeout = value;
}
}
}