nhibernate 2.0有效的数据分页数据列表控件和ObjectDataSource
作者:互联网
我将如何使用nHibernate 2 ObjectDataSource在一次调用中完成Scott的操作
http://weblogs.asp.net/scottgu/archive/2006/01/07/434787.aspx
下面是我的数据访问方法
public IList GetListOfUser(int rows, int pageIndex) {
IList userList = null;
using (ITransaction tx = _session.BeginTransaction()) {
try {
userList = _session.CreateQuery("Select u from User u where u.DateSubmitted is not null")
.SetFirstResult(rows * (pageIndex - 1) + 1)
.SetMaxResults(rows)
.List();
tx.Commit();
} catch (NHibernate.HibernateException ex) {
tx.Rollback();
AppUtil.LogHelper.WriteLog(LogLevel.ERROR, ex.ToString());
throw;
}
}
return userList;
}
解决方法:
实际上,如果使用ICriteria查询,则可以使用此帮助程序方法在到服务器的一次往返中获得结果页面和总记录数:
protected IList<T> GetByCriteria(
ICriteria criteria,
int pageIndex,
int pageSize,
out long totalCount)
{
ICriteria recordsCriteria = CriteriaTransformer.Clone(criteria);
// Paging.
recordsCriteria.SetFirstResult(pageIndex * pageSize);
recordsCriteria.SetMaxResults(pageSize);
// Count criteria.
ICriteria countCriteria = CriteriaTransformer.TransformToRowCount(criteria);
// Perform multi criteria to get both results and count in one trip to the database.
IMultiCriteria multiCriteria = Session.CreateMultiCriteria();
multiCriteria.Add(recordsCriteria);
multiCriteria.Add(countCriteria);
IList multiResult = multiCriteria.List();
IList untypedRecords = multiResult[0] as IList;
IList<T> records = new List<T>();
if (untypedRecords != null)
{
foreach (T obj in untypedRecords)
{
records.Add(obj);
}
}
else
{
records = new List<T>();
}
totalCount = Convert.ToInt64(((IList)multiResult[1])[0]);
return records;
}
它会克隆您的原始条件两次:一项返回页面记录的条件,另一项返回总记录数的条件.它还使用IMultiCriteria在一次往返中执行两个数据库调用.
标签:objectdatasource,nhibernate,asp-net,c 来源: https://codeday.me/bug/20191209/2096979.html