我正在使用以下代码,想知道如果使用企业库的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,在正常情况下,使用“Database”类提供的方法调用数据库操作可以满足需求。在某些情况下,对于长时间运行的查询,开发人员希望设置 SqlCommand(或 DbCommand)类的 CommandTimeout 属性。这将允许查询按照命令超时中设置的值长时间执行。
默认情况下,数据访问应用程序块不支持/在方法调用中采用简单的 CommandTimeout 参数(网上有许多可用的解决方法示例)。为了以最小的更改实现相同的目的,我添加了一个名为“WithCommandTimeOut”的简单函数,在“Microsoft.Practices.EnterpriseLibrary.Data.Database”类中采用 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, 查询);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)