来源:远方网络 | 2005-12-31 9:40:19 | (有1709人读过)
5.2 设计一个数据库查询器
例17.2:在数据库查询器中,用户可以选择要查询的数据库,查询数据库中的那一个表、根据数据库表中那一个字段进行查询,并且可以方便地指定查询条件,指定查询条件主要包括指定逻辑运算符(=、>、<、<=、>=、like、in、NOT like、NOT in)和字段值。
例子全部的程序**如下:
unit main;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, DB, DBTables, Buttons, ComCtrls, Tabnotbk;
type
TQueryForm = class(TForm)
BitBtn1: TBitBtn;
DataSource1: TDataSource;
Table1: TTable;
GroupBox1: TGroupBox;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
Label5: TLabel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
ListBox1: TListBox;
ListBox2: TListBox;
ListBox3: TListBox;
Edit1: TEdit;
ComboBox1: TComboBox;
BitBtn2: TBitBtn;
TabSheet2: TTabSheet;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure ListBox2Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
end;
var
QueryForm: TQueryForm;
implementation
{$R *.DFM}
uses RSLTFORM;
procedure TQueryForm.FormCreate(Sender: TObject);
begin
Screen.Cursor := crHourglass;
{ Populate the alias list }
with ListBox1 do
begin
Items.Clear;
Session.GetAliasNames(Items);
end;
{ Make sure there are aliases defined }
Screen.Cursor := crDefault;
if ListBox1.Items.Count < 1 then
MessageDlg( 'There are no database aliases currently defined. You need at least one alias to use this demonstration.',
mtError, [mbOK], 0 );
{ Default the drop-down list to the first value in the list }
ComboBox1.ItemIndex := 0;
end;
procedure TQueryForm.ListBox1Click(Sender: TObject);
var
strValue: string; { Holds the alias selected by the user }
bIsLocal: Boolean; { Indicates whether or not an alias is local }
slParams: TStringList; { Holds the parameters of the selected alias }
iCounter: Integer; { An integer counter variable for loops}
begin
{ Determine the alias name selected by the user }
with ListBox1 do
strValue := Items.Strings[ItemIndex];
{ Get the names of the tables in the alias and put them in the appropriate list box, making sure the user's choices are reflected
in the list. }
ListBox2.Items.Clear;
Session.GetTableNames(strValue, { alias to enumerate }
'', { pattern to match }
CheckBox1.Checked, { show extensions flag }
CheckBox2.Checked, { show system tables flag }
ListBox2.Items); { target for table list }
{ Make sure there are tables defined in the alias. If not, show an
error; otherwise, clear the list box. }
Screen.Cursor := crDefault;
if ListBox2.Items.Count < 1 then
MessageDlg('There are no tables in the alias you selected. Please
choose another', mtError, [mbOK], 0 );
ListBox3.Items.Clear;
end;
procedure TQueryForm.ListBox2Click(Sender: TObject);
begin
Screen.Cursor := crHourglass;
try
{ First, disable the TTable object. }
if Table1.Active then
Table1.Close;
{ Open the selected table }
with ListBox1 do
Table1.DatabaseName := Items.Strings[ItemIndex];
with ListBox2 do
Table1.TableName := Items.Strings[ItemIndex];
{ Open the table and put a list of the field names in the Fieldslist box. }
|