为所有新命令对象设置自定义默认CommandTimeout [英] Set custom default CommandTimeout for all new Command Objects

查看:79
本文介绍了为所有新命令对象设置自定义默认CommandTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CommandTimeout 的默认值为30秒.您可以通过执行以下操作手动更改命令对象实例上的值

The default CommandTimeout value is 30 seconds. You can manually change the value on an instance of the command object by doing the following

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?

推荐答案

据我所知,没有办法更改此默认设置.SqlCommand的构造函数代码是这样的:

As far as I know no, there is no way to change this default. The constructor code for a SqlCommand is this:

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);
}

可能的解决方法是使用预定义的SqlCommand作为参数传递给以SqlCommand作为参数的构造函数.

A possible workaround is to use a predefined SqlCommand passed as argument to the constructor that takes a SqlCommand as argument.

因此您可以创建全局SqlCommand(模板)

So you could create a global SqlCommand (a template)

 Dim commandTemplate60 = new SqlCommand()
 commandTemplate60.Timeout = 60

,然后在需要60秒超时的命令时

and then when you need a command with a timeout of 60 seconds

Dim cmd As New System.Data.SqlClient.SqlCommand(commandTemplate60)

但是,直接设置超时时间似乎并没有很多工作

However, setting directly the Timeout, doesn't seems to be a lot of work

这篇关于为所有新命令对象设置自定义默认CommandTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆