编程从数据库搜索关键字?

时间:2019-06-17 02:32:13

标签: c# sql

你好,我有一个包含以下各列的表。

Profile_ID,
Name,
Age,
More_Info_about family,
qualification details, 
job details and 
partner_preference

我必须根据关键字进行搜索,关键字将从文本框中获取。

搜索结果应采用以下方式:如果关键字出现在任何条件的开头,结尾和中间,则出现在任何条件下。

我尝试了LIKE'“ + txtbox_keyword.Text +”' 但如果在sendense之间存在keyowrd,则不能正常工作,也就不能搜索数据。

select (Profile_ID,Name,Age, More_info_about_family,Job_Business_Location_City,Salary 
from tblRegistration  
WHERE More_info_about_family LIKE '" + txtbox_keyword.Text + "' 
   OR LIKE '" + txtbox_keyword.Text + "' 
   OR origin LIKE '" + txtbox_keyword.Text + "' 
   OR Job_Detail LIKE '" + txtbox_keyword.Text + "' ", con);

1 个答案:

答案 0 :(得分:1)

使用sqlParameter进行除sql注入以外的操作,您需要在查询中添加%%才能按字段搜索。

SqlParameter parameter = new SqlParameter("@keyWord",txtbox_keyword.Text);

select (Profile_ID,Name,Age, More_info_about_family,Job_Business_Location_City,Salary 
from tblRegistration  
WHERE More_info_about_family LIKE '%@keyWord%' 
   OR LIKE '%@keyWord%' 
   OR origin LIKE '%@keyWord%' 
   OR Job_Detail LIKE '%@keyWord%', con,parameter);
相关问题