Sqlite 參數化 模糊查詢 解決方案

轉自:https://codedefault.com/2018/does-dapper-support-the-like-operator-in-csharp-application

問題描述

如題,在.NET/C#的程序開發中,使用Dapper查詢數據時,如何實現相似SQL查詢語句中的like操做,如:php

var data = conn.Query(@"
    select top 25 Term as Label, Type, ID from SearchTerms WHERE Term like '%@T%'", new { T = (string)term }); 

以上的語句執行後的結果是錯誤的,在Dapper中,相似SQL語句中的like應該如何處理呢?sql

方案一

term = "whateverterm"; var encodeForLike = term => term.Replace("[", "[[]").Replace("%", "[%]"); string term = "%" + encodeForLike(term) + "%"; var data = conn.Query(@" select top 25 Term as Label, Type, ID from SearchTerms WHERE Term like @term", new { term }); 

方案二

string query = "SELECT * from country WHERE Name LIKE CONCAT('%',@name,'%');" var results = connection.query<country>(query, new {name}); 

方案三

db.Query<Remitente>("SELECT * FROM Remitentes WHERE Nombre LIKE @n", new { n = "%" + nombre + "%" }) .ToList();
相關文章
相關標籤/搜索