数据库搜索框

时间:2013-03-16 10:02:57

标签: delphi function

你好,我刚刚开始使用delphi 7,我编写了一个应用程序来管理我的mdb数据库。我只想放一个搜索框,如果我输入一个关键字,它将在数据库的特定行上返回带有关键字的结果。

示例:在名为first name的行上我想用john关键字搜索数据库然后当我点击enter或search按钮时,应用程序将返回包含名字中包含john的所有数据的结果

type
    Tcollector = class(TForm)
    Image1: TImage;
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  collector: Tcollector;

implementation

{$R *.dfm}

procedure Tcollector.DataSource1DataChange(Sender: TObject; Field: TField);
begin

end;

编辑:

我做到了这一点:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls;

type
    TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    ADOQuery4: TADOQuery;
    ADOQuery5: TADOQuery;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ADOConnection1.GetTableNames(ComboBox1.Items);
end;

procedure TForm1.Button1Click(Sender: TObject);
var  tblname : string;
begin

if ComboBox1.ItemIndex < 0 then Exit;
tblname := ComboBox1.Items[ComboBox1.ItemIndex];

with ADOQuery1 do begin
Close;
  SQL.Text := 'SELECT * FROM ' + tblname;
  Open;
end;

with ADOQuery2 do begin
  Close;
  SQL.Text := 'SELECT * FROM ' + tblname;
  Open;
end;

with ADOQuery3 do begin
  Close;
  SQL.Text := 'SELECT * FROM ' + tblname;
  Open;
end;

with ADOQuery4 do begin
  Close;
  SQL.Text := 'SELECT * FROM ' + tblname;
  Open;
end;

with ADOQuery5 do begin
  Close;
  SQL.Text := 'SELECT * FROM ' + tblname;
  Open;
end;

    end;
procedure TForm1.Button2Click(Sender: TObject);
begin
form2.show;
end;

end.

到目前为止,我可以提取所有表格数据。我想让我的程序做的是显示我在tedit上输入的数据

对不起我的第一篇帖子我还不熟悉论坛的快捷方式和发布规则。 :d

2 个答案:

答案 0 :(得分:1)

TDataSet.Filter 要么 TDataSet.OnFilterRecord 或直接使用SQL。

答案 1 :(得分:0)

只是一些小问题,但也许我可以搞清楚

begin
ADOTable1.First;
if ADOTable1.Locate('Last',edit1.Text ,[]) then begin
Label1.Caption := ADOTable1.FieldByName('Last').AsString;
Label2.Caption := ADOTable1.FieldByName('First').AsString;
Label3.Caption := ADOTable1.FieldByName('address').AsString;
Next;
end else begin
Label1.Caption := '';
Label2.Caption := '';
Label3.Caption := '';