我已经和PetaPoco进行了两天的实施搜索解决方案(显然有一些搜索参数),这些解决方案在数据库中有自定义分页。我无法弄清楚如何使用ASP.NET DataPager配置PetaPoco分页(这是一个完全不同的问题)。无论如何,我想在数据库中使用自定义分页。
我有一个名为GetUsersPaged
的存储过程,如下所示:
ALTER PROCEDURE [dbo].[GetUsersPaged]
@startRowIndex int,
@maximumRows int,
@name nvarchar(300) = NULL,
@email nvarchar(100) = NULL
AS
BEGIN
SELECT *
FROM
(
SELECT *,
ROW_NUMBER() OVER(ORDER BY Id) AS RowRank
FROM UserInfo
WHERE
(Nickname LIKE '%'+@name+'%'
OR FirstName LIKE '%'+@name+'%'
OR LastName LIKE '%'+@name+'%'
OR @name IS NULL)
AND
(Email = @email OR @email IS NULL)
) AS UsersPagedList
WHERE RowRank BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows)
END
和GetUsersCount
存储过程如下所示:
ALTER PROCEDURE [dbo].[GetUsersCount]
@name nvarchar(300) = NULL,
@email nvarchar(100) = NULL
AS
BEGIN
SELECT COUNT(*)
FROM UserInfo
WHERE
(Nickname LIKE '%'+@name+'%'
OR FirstName LIKE '%'+@name+'%'
OR LastName LIKE '%'+@name+'%'
OR @name IS NULL)
AND
(Email = @email OR @email IS NULL)
END
现在我有两种调用这些存储过程的方法,如下所示:
[DataObjectMethod(DataObjectMethodType.Select, false)]
public List<DAL.UserInfo> GetPagedUserSearchResults(int startRowIndex, int pageSize, string name, string email)
{
DBService dbService = new DBService();
var db = dbService.GetDatabase();
var list = new List<DAL.UserInfo>();
if(name != string.Empty && email != string.Empty)
list = db.Fetch<DAL.UserInfo>(@"EXEC GetUsersPaged @@startRowIndex = @0, @@maximumRows = @1,
@@name = @2, @@email = @3", startRowIndex, pageSize, name, email);
else if(name == string.Empty && email != string.Empty)
list = db.Fetch<DAL.UserInfo>(@"EXEC GetUsersPaged @@startRowIndex = @0, @@maximumRows = @1,
@@email = @2", startRowIndex, pageSize, email);
else if(name != string.Empty && email == string.Empty)
list = db.Fetch<DAL.UserInfo>(@"EXEC GetUsersPaged @@startRowIndex = @0, @@maximumRows = @1,
@@name = @2", startRowIndex, pageSize, name);
else if(name == string.Empty && email == string.Empty)
list = db.Fetch<DAL.UserInfo>(@"EXEC GetUsersPaged @@startRowIndex = @0, @@maximumRows = @1"
,startRowIndex, pageSize);
return list;
}
[DataObjectMethod(DataObjectMethodType.Select, false)]
public int GetPagedUserSearchResultsCount(string name, string email)
{
DBService dbService = new DBService();
var db = dbService.GetDatabase();
IEnumerable<DAL.UserInfo> count = null;
if (name != string.Empty && email != string.Empty)
count = db.Query<DAL.UserInfo>("EXEC GetUsersCount @@name = @0, @@email = @1", name, email);
else if (name == string.Empty && email != string.Empty)
count = db.Query<DAL.UserInfo>("EXEC GetUsersCount @@email = @0", email);
else if(name != string.Empty && email == string.Empty)
count = db.Query<DAL.UserInfo>("EXEC GetUsersCount @@name = @0", name);
else if (name == string.Empty && email == string.Empty)
count = db.Query<DAL.UserInfo>("EXEC GetUsersCount");
return count.Count<DAL.UserInfo>();
}
我的问题是:当我在db(在SQL Management Studio中)执行和测试存储过程时,一切都很好。所有结果都与寻呼参数有关。但是当我使用PetaPoco调用相同的存储过程时,它会取出整个内容并忽略分页。
例如,当我使用
在SQL Management Studio中测试存储过程时@startRowIndex = 0
@maximumRows = 1
并且我没有为@name
和@email
发送任何内容,它只返回一行,这是完全正确的。但与PetaPoco相同的是,它在列表中返回3行。知道问题是什么吗?
答案 0 :(得分:0)
您是否尝试过设置EnableAutoSelect = false?当我将其保留为默认设置时,该调用将返回所有记录。