vb.net 为所有新命令对象设置自定义默认命令超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18089812/
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
Set custom default CommandTimeout for all new Command Objects
提问by KyleMit
The default CommandTimeoutvalue is 30 seconds. You can manually change the value on an instance of the command object by doing the following
默认的CommandTimeout值为 30 秒。您可以通过执行以下操作手动更改命令对象实例上的值
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.CommandTimeout = 60
Is there a way to specify a different default value, such that all new command objects will automatically have this value within your solution when they are created?
有没有办法指定不同的默认值,这样所有新命令对象在创建时都会在您的解决方案中自动具有此值?
采纳答案by Steve
As far as I know no, there is no way to change this default. The constructor code for a SqlCommand is this:
据我所知,没有办法改变这个默认值。SqlCommand 的构造函数代码是这样的:
public SqlCommand()
{
this.ObjectID = Interlocked.Increment(ref _objectTypeCount);
this._commandTimeout = 30;
this._updatedRowSource = UpdateRowSource.Both;
this._prepareHandle = -1;
this._rowsAffected = -1;
this._notificationAutoEnlist = true;
GC.SuppressFinalize(this);
}
A possible workaround is to use a predefined SqlCommand passed as argument to the constructor that takes a SqlCommand as argument.
一种可能的解决方法是使用预定义的 SqlCommand 作为参数传递给以 SqlCommand 作为参数的构造函数。
So you could create a global SqlCommand (a template)
所以你可以创建一个全局的 SqlCommand(一个模板)
Dim commandTemplate60 = new SqlCommand()
commandTemplate60.Timeout = 60
and then when you need a command with a timeout of 60 seconds
然后当你需要一个超时为 60 秒的命令时
Dim cmd As New System.Data.SqlClient.SqlCommand(commandTemplate60)
However, setting directly the Timeout, doesn't seems to be a lot of work
不过,直接设置Timeout,好像工作量不大

