使用方法“CreateSqlStringAccessor”时如何设置“超时”?

时间:2012-11-29 19:52:32

标签: .net enterprise-library command-timeout

我有这样的代码,如何设置“CommandTimeout”?

var database = EnterpriseLibraryContainer.Current.GetInstance<Database>();
var sqlGetAllPersons = @"select * from Person.Person";
var personMapper = MapBuilder<Person>.MapAllProperties().Build();

// create an accessor (DataAccessor)
var personAccessor = database.CreateSqlStringAccessor<Person>(sqlGetAllPersons, personMapper);

// execute the accessor (IEnumerable<Person>)
var profiles = personAccessor.Execute();

(来自EntLib 5.0 DAAB MapBuilder maps DBNull to null的代码)

感谢

1 个答案:

答案 0 :(得分:1)

虽然我不喜欢这个解决方案,但我能够通过这种方式解决它:

public class ParameterMapperWithCommandTimeout : IParameterMapper
{
    public int CommandTimeout { get; set; }

    public void AssignParameters(DbCommand command, object[] parameterValues)
    {
        command.CommandTimeout = this.CommandTimeout;
    }
}

应用于上一个示例:

var database = EnterpriseLibraryContainer.Current.GetInstance<Database>();
var sqlGetAllPersons = @"select * from Person.Person";
var personMapper = MapBuilder<Person>.MapAllProperties().Build();
var parameterMapper = new ParameterMapperWithCommandTimeout { CommandTimeout = MyTimeOut };

// create an accessor (DataAccessor)
var personAccessor = database.CreateSqlStringAccessor<Person>(sqlGetAllPersons, parameterMapper, personMapper);

// execute the accessor (IEnumerable<Person>)
var profiles = personAccessor.Execute();