开发者

Parameterized LIKE clause in SQL statement using Dapper

I want to perform the following query using Dapper, which curr开发者_运维知识库ently doesn't return expected results (I think it must be treating the @pName param as literal text within the single quotes?):

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

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

Things work if I just build the SQL like:

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?


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


I would like to add here another possible solution:

var results = cn.Query("SELECT * FROM Table WHERE Column LIKE @value", new { value = value + "%" });

The wildcard is inside the string var itself, and then we reference that var in the SQL. Applies to any wildcard pattern you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜