GEN_Q.PAS
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:6k
- unit Gen_Q;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, DB, DBTables, StdCtrls, Buttons, ExtCtrls, ComCtrls, DBClient;
- type
- TF_Query = class(TForm)
- StatusBar1: TStatusBar;
- Panel2: TPanel;
- btn_cs: TSpeedButton;
- btn_bq: TSpeedButton;
- btn_hz: TSpeedButton;
- btn_qktj: TSpeedButton;
- btn_jgxs: TSpeedButton;
- btn_fq: TSpeedButton;
- Panel1: TPanel;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- btn_ListAll: TSpeedButton;
- Cmb_zdm: TComboBox;
- Cmb_czf: TComboBox;
- Cmb_zdz: TComboBox;
- Memo1: TMemo;
- procedure btn_csClick(Sender: TObject);
- procedure btn_bqClick(Sender: TObject);
- procedure btn_hzClick(Sender: TObject);
- procedure btn_qktjClick(Sender: TObject);
- procedure btn_jgxsClick(Sender: TObject);
- procedure btn_fqClick(Sender: TObject);
- procedure btn_ListAllClick(Sender: TObject);
- procedure btn_valid;
- procedure Cmb_zdmChange(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- private
- { Private declarations }
- public
- AppServName: TCustomRemoteServer; //应用服务器连接的名字
- ProviderName: string; //应用服务器数据提供的接口
- TableName: string; //查询的数据表名
- fields: TStrings; //查询的字段列表
- fieldsAlias: TStrings; //查询的字段列表相对应的别名
- Conf: boolean; //判断是否按下确认按钮,还是放弃
- filtervalue: string; //过滤的条件值
- { Public declarations }
- end;
- var
- F_Query: TF_Query;
- implementation
- {$R *.dfm}
- //用于互置按钮的有效性
- procedure TF_Query.btn_valid;
- begin
- btn_cs.enabled := not btn_cs.enabled;
- btn_bq.enabled := not btn_bq.enabled;
- btn_hz.enabled := not btn_hz.enabled;
- btn_qktj.enabled := not btn_qktj.enabled;
- btn_jgxs.enabled := not btn_jgxs.enabled;
- end;
- //当窗体创建时,初始化相关资源
- procedure TF_Query.FormCreate(Sender: TObject);
- begin
- fields := Tstringlist.Create;
- fieldsAlias := TStringList.Create;
- Conf := False;
- //向操作符下拉框添加操作符
- cmb_czf.items.Clear;
- cmb_czf.Items.Add('=');
- cmb_czf.Items.Add('>');
- cmb_czf.Items.Add('>=');
- cmb_czf.Items.Add('<');
- cmb_czf.Items.Add('<=');
- cmb_czf.Items.Add('<>');
- cmb_czf.Items.Add('like');
- end;
- //窗体关闭,释放相关资源
- procedure TF_Query.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- fields.free;
- fieldsAlias.free;
- end;
- //当窗体激活时,向字段名下列框添加值
- procedure TF_Query.FormActivate(Sender: TObject);
- begin
- cmb_zdm.Items.AddStrings(fieldsAlias);
- end;
- //初始按钮点击过程
- procedure TF_Query.btn_csClick(Sender: TObject);
- var
- xx101: string;
- xx101Name: string;
- xx201: string;
- xx301: string;
- begin
- if trim(cmb_zdm.Text) = '' then
- begin
- showmessage('字段名不能为空');
- exit;
- end;
- if trim(cmb_czf.Text) = '' then
- begin
- showmessage('操作符不能为空');
- exit;
- end;
- xx101 := Cmb_zdm.items.strings[cmb_zdm.itemindex];
- xx101Name := fields[cmb_zdm.Itemindex];
- xx201 := cmb_czf.items.strings[cmb_czf.itemindex];
- if xx201 = 'like' then
- xx301 := '%' + Cmb_zdz.text + '%'
- else
- xx301 := cmb_zdz.text;
- filtervalue := xx101Name + ' ' + xx201 + ' ''' + xx301 + ''' ';
- memo1.lines.add(xx101 + ' ' + xx201 + ' ' + cmb_zdz.text);
- btn_valid;
- end;
- //并且按钮点击过程
- procedure TF_Query.btn_bqClick(Sender: TObject);
- var
- xx102: string;
- xx102Name: string;
- xx202: string;
- xx302: string;
- begin
- if trim(cmb_zdm.Text) = '' then
- begin
- showmessage('字段名不能为空');
- exit;
- end;
- if trim(cmb_czf.Text) = '' then
- begin
- showmessage('操作符不能为空');
- exit;
- end;
- xx102 := cmb_zdm.items.strings[cmb_zdm.itemindex];
- xx102Name := fields[cmb_zdm.itemindex];
- xx202 := cmb_czf.items.strings[cmb_czf.itemindex];
- if xx202 = 'like' then
- xx302 := '%' + cmb_zdz.Text + '%'
- else
- xx302 := cmb_zdz.Text;
- filtervalue := filtervalue + ' and ' + xx102Name + ' ' + xx202 + ' ''' + xx302 + ''' ';
- memo1.lines.add('并且 ' + xx102 + ' ' + xx202 + ' ' + cmb_zdz.text);
- end;
- //或者按钮点击过程
- procedure TF_Query.btn_hzClick(Sender: TObject);
- var
- xx103: string;
- xx103Name: string;
- xx203: string;
- xx303: string;
- begin
- if trim(cmb_zdm.Text) = '' then
- begin
- showmessage('字段名不能为空');
- exit;
- end;
- if trim(cmb_czf.Text) = '' then
- begin
- showmessage('操作符不能为空');
- exit;
- end;
- xx103 := cmb_zdm.items.strings[cmb_zdm.itemindex];
- xx103Name := fields[cmb_zdm.itemindex];
- xx203 := cmb_czf.items.strings[cmb_czf.itemindex];
- if xx203 = 'like' then
- xx303 := '%' + cmb_zdz.Text + '%'
- else
- xx303 := cmb_zdz.Text;
- filtervalue := filtervalue + ' or ' + xx103Name + ' ' + xx203 + ' ''' + xx303 + ''' ';
- memo1.lines.add('或者 ' + xx103 + ' ' + xx203 + ' ' + cmb_zdz.text);
- end;
- //清空条件按钮点击过程
- procedure TF_Query.btn_qktjClick(Sender: TObject);
- begin
- memo1.lines.clear;
- cmb_zdm.setfocus;
- btn_valid;
- end;
- //结果显示按钮点击过程
- procedure TF_Query.btn_jgxsClick(Sender: TObject);
- begin
- Conf := True;
- Close;
- end;
- //放弃按钮点击过程
- procedure TF_Query.btn_fqClick(Sender: TObject);
- begin
- Close;
- end;
- //根据字段名列表框列出数据库中存在的值
- procedure TF_Query.btn_ListAllClick(Sender: TObject);
- var
- s_value, s_value1: string;
- generalquery: TClientDataset;
- begin
- cmb_zdz.items.clear;
- if cmb_zdm.ItemIndex < 0 then
- exit;
- s_value := fields[cmb_zdm.itemindex];
- generalquery := TClientDataset.Create(nil);
- generalquery.ProviderName := ProviderName;
- generalQuery.RemoteServer := TCustomRemoteServer(AppServName);
- generalquery.close;
- generalquery.CommandText := 'select distinct ' + s_value + ' from ' + tablename;
- try
- generalquery.open;
- except
- ShowMessage('列举出错');
- exit;
- end;
- generalquery.First;
- while not generalquery.eof do
- begin
- s_value1 := generalquery.fieldbyname(s_value).asstring;
- cmb_zdz.Items.Add(s_value1);
- generalquery.Next;
- end;
- generalquery.close;
- generalquery.Free;
- end;
- //当字段名改变时,清空字段值下拉框
- procedure TF_Query.Cmb_zdmChange(Sender: TObject);
- begin
- cmb_zdz.Items.Clear;
- end;
- end.