如何使用组合框内的Like关键字 [英] How to use Like keyword which is inside of combobox

查看:107
本文介绍了如何使用组合框内的Like关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格视图,其中包含一个包含
的行 1.用于选择列的组合框
< big> 2.用于搜索方法(=,< ;,<> ;,>)</big>
的Combobx 3.搜索文本框(搜索内容)
4.运算符组合框(AND/OR)

仔细看这段代码片段

I have a grid view which include a row that contain
1. Combobox for column selection
<big>2. Combobx for searching method (=, like, <, <>, >)</big>
3. Text box for search (what to search)
4. Combobox for operator (AND/OR)

look carefully this code fragment

if (dataGridView1.RowCount > 1) //When DataGridview Contain MOre then 1 row
                {
                    if (Datarow.Cells[0].Value != null && Datarow.Cells[1].Value != null
                    && Datarow.Cells[2].Value != null && Datarow.Cells[3].Value != null)
                    {
WhereClause += Datarow.Cells[0].Value.ToString() + "  " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString()+ " " + Datarow.Cells[3].Value.ToString()+ " ";
                        SearchQuery =  WhereClause;
                    }


我正在用它来构成一个Where子句.但是Gridview的第二列在Combobox#2内并且包含(=,如< ;,<> ;,>),这对我来说确实是一个问题


I am using this to Form a Where Clause. but Gridview''s 2nd column which inside Combobox # 2 and it contain (=, like, <, <>, >) which is really problem form me

Datarow.Cells[1].Value.ToString()


其中包含=,<>,<,>和最重要的 like
现在我该如何使用像关键词一样的东西.上面的代码片段可以与=,<>,<,>一起正常工作.但是我想与%一起使用的原因是什么,但我不知道如何使用


this contain =, <>, <, >, and most important like
now how can i use like key word. The above fragment of code working fine with =, <>, <,> but what for Like i want to use with % but i dont know how to it

推荐答案

您将必须对其进行特殊对待.喜欢:
You''ll have to treat it specially. Like:
const string LikeMethodName = 'like';
WhereClause += Datarow.Cells[0].Value.ToString() + "  ";
string method = Datarow.Cells[1].Value.ToString();
if (method.Equals(LikeMethodName, StringComparison.OrdinalIgnoreCase))
{
  WhereClause += "like '%" + Datarow.Cells[2].Value.ToString() + "%' ";
}
else
{
  WhereClause += method + " " + Datarow.Cells[2].Value.ToString() + " ";
}
WhereClause += Datarow.Cells[3].Value.ToString() + " ";
SearchQuery = WhereClause;



假设用于构造查询的所有 all 值是您定义的列表中的选择,那么就可以了.如果输入到查询中的任何值是用户键入"的,则非常容易受到



Assuming that all of the values you are using to construct the query are selections from lists that you define, then this is OK. If any value going into the query is "typed-in" by the user, then this is very vulnerable to SQL injection attacks[^].

In any case, instead of repeated string concatenation, I suggest using System.Text.StringBuilder for assembling a string from multiple pieces.


在"if"子句中检查"like",如下所示. ..
Check for "like" in a "if" clause something like below...
if(Datarow.Cells[1].Value.ToString().ToLower().Equals("like"))
{
    WhereClause += Datarow.Cells[0].Value.ToString() + "  " + "like '%" + Datarow.Cells[2].Value.ToString()+ "%' " + Datarow.Cells[3].Value.ToString()+ " ";
}
else
{
    WhereClause += Datarow.Cells[0].Value.ToString() + "  " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString()+ " " + Datarow.Cells[3].Value.ToString()+ " ";
}


这篇关于如何使用组合框内的Like关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆