我需要尝试部分搜索用户,现在,我的代码只是根据EXACT用户名查找结果。我怎么能这样做?
我的代码可以在下面找到,请注意我正在使用“@ 0”,这似乎阻止我使用%@ 0%。
Here's my code:
@{
Layout = "~/Admin/_SiteLayout.cshtml";
var db = Database.Open("MikZeRCoding2");
var userSearchQuery = "SELECT * FROM [Users] WHERE UserName = @0";
var UsernameSearch = "";
var ErrorMessage = "";
var searchString = Request.Form["search-username"];
if (IsPost) {
UsernameSearch = Request.Form["search-username"];
if (UsernameSearch.IsEmpty()) {
ErrorMessage = "You didn't search for anything.";
}
if (!UsernameSearch.IsEmpty() && db.QueryValue(userSearchQuery, UsernameSearch) == null) {
ErrorMessage = "No results for '" + searchString + "' were found...";
}
if (searchString == "bitch") {
ErrorMessage = "Bitch spotted in-front of your pc monitor.";
}
else {
}
}
}
<form method="post" action="">
<div class="input-group">
<input type="text" name="search-username" placeholder="Search for a user" class="form-control" />
<span class="input-group-btn">
<button class="btn btn-default glyphicon glyphicon-search" type="button"></button>
</span>
</div>
<input type="submit" value="Search" />
@foreach(var user in db.Query(userSearchQuery, UsernameSearch)) {
<a href="@Href("~/Admin/Accounts/User", user.UserId)"><span>[ @user.UserId ]</span> @user.UserName</a>
}
@if(!ErrorMessage.IsEmpty()) {
<div class="alert alert-danger">@ErrorMessage</div>
}
</form>
答案 0 :(得分:0)
我认为你应该可以做到:
var userSearchQuery = "SELECT * FROM [Users] WHERE UserName LIKE '%' + @0 + '%'";
编辑:
@0
为空时不返回任何内容:
var userSearchQuery = "SELECT * FROM [Users] WHERE UserName LIKE '%' + @0 + '%' and @0 <> ''";