使用Dapper的SQL语句中的参数化LIKE子句

时间:2021-08-12 01:37:55

I want to perform the following query using Dapper, which currently doesn't return expected results (I think it must be treating the @pName param as literal text within the single quotes?):

我想使用Dapper执行以下查询,Dapper当前不返回预期结果(我认为它必须将@pName参数视为单引号中的文字文本?):

var q = "SELECT * FROM Users WHERE Name LIKE '@pName%'";

@pName is the param I assign a value to upon executing the query.

@pName是我在执行查询时为其分配值的参数。

Things work if I just build the SQL like:

如果我只是构建SQL,事情就会起作用:

var q = "SELECT * FROM Users WHERE Name LIKE '" + name + "%'";

.. but I would prefer to use a param if possible.

..但如果可能,我宁愿使用参数。

I am executing the query using the following code:

我正在使用以下代码执行查询:

o = _cn.Query<User>(q, new { pName = new DbString { Value = name, IsFixedLength = false, Length = 25, IsAnsi = true } }).ToList();

How do I got about this using Dapper?

我如何使用Dapper解决这个问题?

1 个解决方案

#1


15  

SELECT * FROM Users WHERE Name LIKE @pName + '%'

#1


15  

SELECT * FROM Users WHERE Name LIKE @pName + '%'