这里遇到这样的问题:有一个返回SQL函数选择结果的方法,当没有WHERE子句调用函数时一切正常,但是当用@search调用它时我没有得到任何结果,但同样的查询工作完美MsSQL经理。
(Visual Studio Enterprise 2015,MsSqlServer Express 2016)
public async Task<IEnumerable<SelectedJob>> GetFunction(int? moduleId, int? specialGroupId, string search)
{
var commandText = "SELECT * FROM GUI.fGetSelectedJobs(@ModuleID, @SpecialGroupID)";
var parameters = new Dictionary<string, object> { { "@ModuleID", moduleId }, { "@SpecialGroupID", specialGroupId } };
if (!string.IsNullOrEmpty(search))
{
commandText = "SELECT * FROM GUI.fGetSelectedJobs(@ModuleID, @SpecialGroupID) WHERE JobName LIKE '%@search%'";
parameters = new Dictionary<string, object> { { "@ModuleID", moduleId }, { "@SpecialGroupID", specialGroupId }, { "@search", search } };
}
var rows = await Database.QueryAsync(commandText, parameters);
return rows?.Select(ParseRow).ToList();
}
QueryAsync方法的一部分:
var command = CreateCommand(commandText, parameters);
command.CommandType = commandType;
using (var reader = await command.ExecuteReaderAsync())
while (await reader.ReadAsync())
{
//Some job
}
但是当使用WHERE子句构建命令时它总是为空:
有什么想法吗?
答案 0 :(得分:2)
您的WHERE
部分应为WHERE JobName LIKE @search"
,并在搜索字符串中添加%
:
parameters = new Dictionary<string, object> { { "@ModuleID", moduleId },
{ "@SpecialGroupID", specialGroupId }, { "@search", "%" + search + "%" } };