我正在使用以下代码,想知道如果使用企业库的 CreateSprocAccessor 是否需要设置命令超时,如果不需要,那么如何管理超时?
var accessor = _sqlDatabase.CreateSprocAccessor<xyz>("uspGetxyz",
new xyzParameters(_sqlDatabase),
MapBuilder<xyz>.MapAllProperties().Build());
//Execute the accessor to obtain the results
var Data = accessor.Execute();
xyzList = Data.ToList<xyz>();
最佳答案
我很久以前就开始使用 Microsoft Enterprise Library,在正常情况下,数据库操作调用使用“数据库”类提供的方法来满足需要。在某些情况下,对于长时间运行的查询,开发人员希望设置 SqlCommand(或 DbCommand)类的 CommandTimeout 属性。这将允许查询按照命令超时中设置的值执行很长时间。
默认情况下,数据访问应用程序 block 不支持/在方法调用中采用简单的 CommandTimeout 参数(网络上有许多解决方法示例)。为了以最少的更改实现相同的效果,我在“Microsoft.Practices.EnterpriseLibrary.Data.Database”类中添加了一个名为“WithCommandTimeOut”的简单函数,该函数采用 timeOutSecond 参数,它返回“Database”类的相同实例。请参阅下面更新的代码片段以了解代码更改。希望这会解决超时问题。
//Class Level Static Variables
//Used to reset to default after assigning in "PrepareCommand" static method
static int DEFAULT_COMMAND_TIMEOUT_RESET = 30;
//Default value when "WithCommandTimeOut" not called
static int COMMAND_TIMEOUT_FOR_THIS_CALL = DEFAULT_COMMAND_TIMEOUT_RESET;
public Database WithCommandTimeOut(int timeOutSeconds)
{
COMMAND_TIMEOUT_FOR_THIS_CALL = timeOutSeconds;
return this;
}
protected static void PrepareCommand(DbCommand command, DbConnection connection)
{
if (command == null) throw new ArgumentNullException("command");
if (connection == null) throw new ArgumentNullException("connection");
//Here is the magical code ----------------------------
command.CommandTimeout = COMMAND_TIMEOUT_FOR_THIS_CALL;
//Here is the magical code ----------------------------
command.Connection = connection;
//Resetting value to default as this is static and subsequent
//db calls should work with default timeout i.e. 30
COMMAND_TIMEOUT_FOR_THIS_CALL = DEFAULT_COMMAND_TIMEOUT_RESET;
}
例。 数据库 db = EnterpriseLibraryContainer.Current.GetInstance(Of Database)("SmartSoftware"); db.WithCommandTimeOut(0).ExecuteDataSet(CommandType.Text, query);
关于c# - Enterprise Library 5.0 和命令超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6030297/