代码之家  ›  专栏  ›  技术社区  ›  Abdullah

如何使用LIKE和%%运算符在web表单中查询和筛选我的数据?

c#
  •  0
  • Abdullah  · 技术社区  · 6 年前

    我想使用LIKE和%%operator从我的web表单中的sql server中筛选和选择数据

    我在stackoverflow解决方案中进行了搜索,但我找不到我的案例的解决方案,我尝试了很多,但都没有正常工作。这是我的代码:

    protected void BtnSearch_Click(object sender, EventArgs e)
                {
                    string sql = @" SELECT [appt_id],visitor_name,mobile ,work_place.work_place_name as work_place_name,days.day as day,date,[time],[subject],[duration],appointment_place.appt_place as appt_place 
                                   FROM [Appointments] 
                                   inner join days on days.day_id = Appointments.day_id 
                                   inner join appointment_place on appointment_place.appt_place_id =Appointments.appt_place_id
                                   inner join work_place on work_place.work_place_id = Appointments.work_place_id 
                                   where 1=1 ";
    
                    string condition = "";
                    string orderby = "order by date";
                    // orderby += "order by appointments.time";
    
                    if (txtVisitorName.Text != "")
                    {
                        condition += " and appointments.visitor_name like '" + txtVisitorName.Text + "'";
                    }
                    if (txtsubject.Text != "")
                    {
                        condition += " and appointments.subject like '" + txtsubject.Text + "'";
                    }
                    if (txtMobileNo.Text != "")
                    {
                        condition += " and appointments.mobile ='" + txtMobileNo.Text + "'";
                    }
    
    
                    DataTable dt = func.fireDatatable(string.Format(sql + condition + orderby));
                    gvappt.DataSource = dt;
                    gvappt.DataBind();
                }
    

    我希望代码的输出使用LIKE和%%来搜索访客姓名或主题是否包含部分姓名或部分主题?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Abdullah    6 年前

    我找到了我问题的正确答案:

    if (txtDocSubject.Text != "")
        {
        condition +=" and Documents.Document_subject like '%"+txtDocSubject.Text+"%'";
        }