简体   繁体   English

如何在数据库中搜索特定字段

[英]How to search a specific field in a database

I want the user to be able to choose from a dropdown Combobox listing some of the fields of the database, then below enter a search term and all then all the results that meet the query be displayed in the dbgrid.我希望用户能够从列出数据库某些字段的下拉列表 Combobox 中进行选择,然后在下面输入搜索词,然后所有满足查询的结果都显示在 dbgrid 中。 I'm not sure how to link the current value of the ComboBox into the sql statement.我不确定如何将 ComboBox 的当前值链接到 sql 语句中。 I tried using this我试过用这个

begin
    with ADOQuery do begin
      Close;
      SQL.Clear;
      SQL.Add ('SELECT * FROM List WHERE combobox1.text =' +   QuotedStr (Asearchterm.Text));
      Open;

And it doesn't work.它不起作用。 The error i'm getting is "The parameter combobox1.text has no default value".我得到的错误是“参数 combobox1.text 没有默认值”。 Any ideas?有任何想法吗?

If you're wanting to use the combobox1 text value as part of the sql statement, you'd set up the sql string along the lines of如果您想要将 combobox1 文本值用作 sql 语句的一部分,您可以按照以下行设置 sql 字符串

'SELECT * FROM List WHERE [' + combobox1.text + '] = ''' + QuotedStr(Asearchterm.Text) + ''''

is probably what you're looking for.可能是你要找的。 I added the extra quotes around the QuotedStr because I'm guessing that the filter is not always going to be numeric values.我在 QuotedStr 周围添加了额外的引号,因为我猜测过滤器并不总是数值。 This will work for numeric as well as non-numeric values.这将适用于数字和非数字值。

instead of 'combobox1.text' you have to put actual column name that you are looking for.而不是 'combobox1.text' 你必须输入你正在寻找的实际列名。 And you can use LIKE keyword and some wildcards.您可以使用LIKE关键字和一些通配符。 something like:就像是:

SELECT * FROM 'table WHERE 'column' LIKE '%YOUR_SEARCH_TEXT%';

That was for a search...if you want to find exact string then you have to use = operator instead of LIKE那是为了搜索......如果你想找到确切的字符串,那么你必须使用=运算符而不是 LIKE

More info here更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM