public ActionResult GetExamNameSuggestion(string prefix)
{
//Its not working
string sql = "Select Top 5 ExamName From Qualifications Where ExamName Like '%@ExamName%'";
//Its working
string sql = "Select Top 5 ExamName From Qualifications Where ExamName Like '%S%'";
SqlParameter[] parameters = { new SqlParameter("@ExamName",SqlDbType.VarChar,50){Value=prefix}};
}
任何帮助?
答案 0 :(得分:1)
您需要连接通配符:
string sql = "Select Top 5 ExamName From Qualifications Where ExamName Like '%' + @ExamName + '%'";
如果您使用 SQL Server 2012 + ,请使用:
string sql = "Select Top 5 ExamName From Qualifications Where ExamName Like CONCAT('%', @ExamName, '%')";
使用:'%@ExamName%'
搜索确切的字符串。我猜你在column = '%@ExamName%'
警告:的
请注意,%phrase%
不具备SARG功能,因此查询优化器不会在该列上使用索引(如果存在任何索引)。
在没有明确TOP
的情况下使用ORDER BY
可能会在多次执行之间返回不同的结果。