我有一个表,我试图在其中搜索,它是工作的一切,除了一个已添加使用dropdownlist。
在数据库中,它们被保存为tinyint,所以当我按数字搜索时,它可以工作,但我想按单词搜索。
例如,我使用以下代码初始化它们:
public enum Education : Int16
{
PHD = 1,
}
当我搜索“1”时,它显示其中有PHD的结果,但当我搜索“PHD”时,什么也没有显示。
我正在使用ADO.NET进行CRUD操作
这是我在我的控制器中使用的方法的代码:
string connectionString = Configuration["ConnectionStrings:WebApplication7ContextConnection"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
//SqlDataReader
connection.Open();
string email = User.Identity.Name;
SqlCommand command = new SqlCommand(email, connection);
if(searchString == null)
{
string sql = "Select * From Teacher Where Email = '" + email + "' ORDER BY AddedOn DESC";
SqlCommand command2 = new SqlCommand(sql, connection);
using (SqlDataReader dataReader = command2.ExecuteReader())
{
while (dataReader.Read())
{
ContactUsMessage teacher = new ContactUsMessage();
teacher.Id = Convert.ToInt32(dataReader["Id"]);
teacher.Name = Convert.ToString(dataReader["Name"]);
teacher.Email = Convert.ToString(dataReader["Email"]);
teacher.Phone = Convert.ToString(dataReader["Phone"]);
teacher.education = (Education)Convert.ToInt16(dataReader["Education"]);
teacher.Message = Convert.ToString(dataReader["Message"]);
teacher.AddedOn = Convert.ToDateTime(dataReader["AddedOn"]);
teacherList.Add(teacher);
}
}
}
else
{
string sql = "Select * From Teacher Where Email = '" + email + "' AND education = '"+searchString+"' AND Message LIKE '%"+searchString+"%' OR Name LIKE '%" + searchString + "%' OR Phone LIKE '%" + searchString + "%' ORDER BY AddedOn DESC";
SqlCommand command2 = new SqlCommand(sql, connection);
using (SqlDataReader dataReader = command2.ExecuteReader())
{
while (dataReader.Read())
{
ContactUsMessage teacher = new ContactUsMessage();
teacher.Id = Convert.ToInt32(dataReader["Id"]);
teacher.Name = Convert.ToString(dataReader["Name"]);
teacher.Email = Convert.ToString(dataReader["Email"]);
teacher.Phone = Convert.ToString(dataReader["Phone"]);
teacher.education = (Education)Convert.ToInt16(dataReader["Education"]);
teacher.Message = Convert.ToString(dataReader["Message"]);
teacher.AddedOn = Convert.ToDateTime(dataReader["AddedOn"]);
teacherList.Add(teacher);
}
}
}
connection.Close();
}
return View(teacherList);
1条答案
按热度按时间bqjvbblv1#
您可以使用Select2下拉列表(自动完成+组合框),如下所示:
视图:
控制器: