在 SQL Server 2008 R2 的查询中使用多个 like 子句和 dapper 进行分页
Using multiple like clauses with dapper for paging in a query with SQL Server 2008 R2
我使用 Dapper 在大型 table 中进行分页查询。我使用的函数是这样的:
public List<WarehouseLine> GetPagedWarehouseLines(int pageNumber, int rowsPerPage, string criteria) //todo
{
var query = @"SELECT *
FROM
(SELECT
*,
ROW_NUMBER() OVER (ORDER BY ProductDescr) AS RowNum
FROM
WarehouseLine) AS whl
WHERE
whl.RowNum BETWEEN ((@PageNumber - 1) * @RowsPerPage) + 1 AND @RowsPerPage * (@PageNumber)
AND (@Criteria)
ORDER BY ProductDescr";
return _db.Query<WarehouseLine>(query, new { PageNumber = pageNumber, RowsPerPage = rowsPerPage, Criteria = criteria }).ToList();
}
条件变量是一个字符串,其值如下
(Productdescr like '%%') OR (PartNumber like '%%') OR (SerialNumber like '%%') OR(Manufacturer like '%%') OR (SpecialInstructions like '%%') OR (UDF3 like '%%') OR (producttags like '%%') OR (NotesPerPart like '%%')
抛开括号,查询在 SQL Server Management Studio 中运行良好。但是在调试过程中,出现以下错误:
An expression of non-boolean type specified in a context where a condition is expected, near 'ORDER'.
我使用最新的 stable 版本的 Dapper。
'Criteria' 变量是 SQL 表达式的一部分,它不能被视为 SQL 参数。
var query = @"SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (ORDER BY ProductDescr) AS RowNum
FROM WarehouseLine) AS whl
WHERE whl.RowNum BETWEEN ((@PageNumber-1)*@RowsPerPage)+1
AND @RowsPerPage*(@PageNumber)
AND "
+ Criteria +
@"ORDER BY ProductDescr";
我使用 Dapper 在大型 table 中进行分页查询。我使用的函数是这样的:
public List<WarehouseLine> GetPagedWarehouseLines(int pageNumber, int rowsPerPage, string criteria) //todo
{
var query = @"SELECT *
FROM
(SELECT
*,
ROW_NUMBER() OVER (ORDER BY ProductDescr) AS RowNum
FROM
WarehouseLine) AS whl
WHERE
whl.RowNum BETWEEN ((@PageNumber - 1) * @RowsPerPage) + 1 AND @RowsPerPage * (@PageNumber)
AND (@Criteria)
ORDER BY ProductDescr";
return _db.Query<WarehouseLine>(query, new { PageNumber = pageNumber, RowsPerPage = rowsPerPage, Criteria = criteria }).ToList();
}
条件变量是一个字符串,其值如下
(Productdescr like '%%') OR (PartNumber like '%%') OR (SerialNumber like '%%') OR(Manufacturer like '%%') OR (SpecialInstructions like '%%') OR (UDF3 like '%%') OR (producttags like '%%') OR (NotesPerPart like '%%')
抛开括号,查询在 SQL Server Management Studio 中运行良好。但是在调试过程中,出现以下错误:
An expression of non-boolean type specified in a context where a condition is expected, near 'ORDER'.
我使用最新的 stable 版本的 Dapper。
'Criteria' 变量是 SQL 表达式的一部分,它不能被视为 SQL 参数。
var query = @"SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (ORDER BY ProductDescr) AS RowNum
FROM WarehouseLine) AS whl
WHERE whl.RowNum BETWEEN ((@PageNumber-1)*@RowsPerPage)+1
AND @RowsPerPage*(@PageNumber)
AND "
+ Criteria +
@"ORDER BY ProductDescr";