H
happycyp
Unregistered / Unconfirmed
GUEST, unregistred user!
我想做个动态查询,代码如下:
unit sjcxwh;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, RzDBGrid, RzTabs, ExtCtrls, RzPanel, DB, ADODB,
StdCtrls, DBCtrls, Buttons, DBGridEh, ComCtrls, ToolWin;
type
Tsjwhform = class(TForm)
RzPanel1: TRzPanel;
RzPageControl1: TRzPageControl;
TabSheet1: TRzTabSheet;
TabSheet2: TRzTabSheet;
DataSource1: TDataSource;
ADOQuery1: TADOQuery;
BitBtn1: TBitBtn;
ComboBox1: TComboBox;
RadioGroup1: TRadioGroup;
Edit1: TEdit;
DBGridEh1: TDBGridEh;
BitBtn2: TBitBtn;
CoolBar1: TCoolBar;
ToolBar1: TToolBar;
DBNavigator1: TDBNavigator;
Splitter1: TSplitter;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
BitBtn5: TBitBtn;
GroupBox1: TGroupBox;
Label2: TLabel;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BitBtn1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
sjwhform: Tsjwhform;
implementation
{$R *.dfm}
procedure Tsjwhform.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=cafree;
sjwhform:=nil;
end;
procedure Tsjwhform.BitBtn1Click(Sender: TObject);
begin
adoquery1.Close;
adoquery1.SQL.Clear ;
adoquery1.SQL.Add('select * from employee');
adoquery1.Open ;
end;
procedure Tsjwhform.FormActivate(Sender: TObject);
var
maindir: String;
i: integer;
begin
maindir:=extractfilepath(application.ExeName);
adoquery1.Close;
//adoquery1.ConnectionString:='';
adoquery1.ConnectionString:=format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s/data/data.mdb;Persist Security Info=False',[maindir]);
//Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:/我的软件/xzs/data/data.mdb;Persist Security Info=False
adoquery1.SQL.Clear ;
adoquery1.SQL.Add('select * from employee');
adoquery1.Open ;
with combobox1 do
begin
items.BeginUpdate;
items.clear;
for i:=0 to DBgrideh1.FieldCount-1 do
begin
items.add(DBGridEh1.Columns.Title.Caption);
end;
items.EndUpdate ;
itemindex:=0;
end;
end;
procedure Tsjwhform.BitBtn4Click(Sender: TObject);
begin
Close;
end;
procedure Tsjwhform.BitBtn2Click(Sender: TObject);
var
fieldname: string;
begin
fieldname:=Adoquery1.Fields[combobox1.ItemIndex].DisplayName;
case radiogroup1.ItemIndex of
0: begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from employee where '+fieldname+'=:F1');
adoquery1.Parameters.ParamByName('F1').Value :=edit1.Text;
adoquery1.Open ;
end;
1: begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from employee where '+fieldname+' like :F2');
adoquery1.Parameters.ParamByName('F2').Value :='%'+edit1.Text+'%';
adoquery1.open;
end;
2: begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from employee ');
adoquery1.open;
adoquery1.Locate(fieldname,edit1.Text,[loPartialKey,loCaseInsensitive]);
end;
end;
end;
procedure Tsjwhform.ComboBox1Change(Sender: TObject);
begin
edit1.Text :='';
end;
end.
我的数据库是Aceess,字段名为英文的,
no,name,sex,salary,marriage,tel,department,position.
在程序中的dbgrideh中转换为
中文的:
编号,姓名,工资,婚否,电话,部门,职务。并将这些中文字段添中到combobox1中,以便选择,用来查询。
在查询过程中,用sql查询,第一个和最后一个字段总是出错,其它的没有问题,用locate,就全部没事。
请各位帮忙看看。
unit sjcxwh;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, RzDBGrid, RzTabs, ExtCtrls, RzPanel, DB, ADODB,
StdCtrls, DBCtrls, Buttons, DBGridEh, ComCtrls, ToolWin;
type
Tsjwhform = class(TForm)
RzPanel1: TRzPanel;
RzPageControl1: TRzPageControl;
TabSheet1: TRzTabSheet;
TabSheet2: TRzTabSheet;
DataSource1: TDataSource;
ADOQuery1: TADOQuery;
BitBtn1: TBitBtn;
ComboBox1: TComboBox;
RadioGroup1: TRadioGroup;
Edit1: TEdit;
DBGridEh1: TDBGridEh;
BitBtn2: TBitBtn;
CoolBar1: TCoolBar;
ToolBar1: TToolBar;
DBNavigator1: TDBNavigator;
Splitter1: TSplitter;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
BitBtn5: TBitBtn;
GroupBox1: TGroupBox;
Label2: TLabel;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BitBtn1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
sjwhform: Tsjwhform;
implementation
{$R *.dfm}
procedure Tsjwhform.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=cafree;
sjwhform:=nil;
end;
procedure Tsjwhform.BitBtn1Click(Sender: TObject);
begin
adoquery1.Close;
adoquery1.SQL.Clear ;
adoquery1.SQL.Add('select * from employee');
adoquery1.Open ;
end;
procedure Tsjwhform.FormActivate(Sender: TObject);
var
maindir: String;
i: integer;
begin
maindir:=extractfilepath(application.ExeName);
adoquery1.Close;
//adoquery1.ConnectionString:='';
adoquery1.ConnectionString:=format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s/data/data.mdb;Persist Security Info=False',[maindir]);
//Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:/我的软件/xzs/data/data.mdb;Persist Security Info=False
adoquery1.SQL.Clear ;
adoquery1.SQL.Add('select * from employee');
adoquery1.Open ;
with combobox1 do
begin
items.BeginUpdate;
items.clear;
for i:=0 to DBgrideh1.FieldCount-1 do
begin
items.add(DBGridEh1.Columns.Title.Caption);
end;
items.EndUpdate ;
itemindex:=0;
end;
end;
procedure Tsjwhform.BitBtn4Click(Sender: TObject);
begin
Close;
end;
procedure Tsjwhform.BitBtn2Click(Sender: TObject);
var
fieldname: string;
begin
fieldname:=Adoquery1.Fields[combobox1.ItemIndex].DisplayName;
case radiogroup1.ItemIndex of
0: begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from employee where '+fieldname+'=:F1');
adoquery1.Parameters.ParamByName('F1').Value :=edit1.Text;
adoquery1.Open ;
end;
1: begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from employee where '+fieldname+' like :F2');
adoquery1.Parameters.ParamByName('F2').Value :='%'+edit1.Text+'%';
adoquery1.open;
end;
2: begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from employee ');
adoquery1.open;
adoquery1.Locate(fieldname,edit1.Text,[loPartialKey,loCaseInsensitive]);
end;
end;
end;
procedure Tsjwhform.ComboBox1Change(Sender: TObject);
begin
edit1.Text :='';
end;
end.
我的数据库是Aceess,字段名为英文的,
no,name,sex,salary,marriage,tel,department,position.
在程序中的dbgrideh中转换为
中文的:
编号,姓名,工资,婚否,电话,部门,职务。并将这些中文字段添中到combobox1中,以便选择,用来查询。
在查询过程中,用sql查询,第一个和最后一个字段总是出错,其它的没有问题,用locate,就全部没事。
请各位帮忙看看。