自己想做东东,出了个问题,从没遇到过,求人指点!急!(挑战高手,有本事就给我回复) ( 积分: 50 )

  • 主题发起人 主题发起人 happycyp
  • 开始时间 开始时间
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,就全部没事。

请各位帮忙看看。
 
把你的select * 换成
select no,name,salary,marriage,tel,department,position
或者
select no as 编号,name as 姓名
,salary as 工资,marriage as 婚否,tel as 电话,department as 部门,position as 职务

“编号,姓名,工资,婚否,电话,部门,职务 ”这中间好像漏了一个“sex”
 
To:dawnsong,
问题不在这里,将数据在dbgrideh中,我那个代码没有问题。是执行过程中:

在查询过程中,用sql查询,第一个和最后一个字段总是出错,其它的没有问题,用locate,就全部没事。

“编号,姓名,工资,婚否,电话,部门,职务 ”应为:
“编号,姓名,性别,工资,婚否,电话,部门,职务 ”
少写了一个。
 
第一个和最后一个[red]字段[/red]总是出错,其它的没有问题,用locate,就全部没事
-----------------------------------------------------------------
是记录record 吧,不应该是Field字段吧?(都用locate了)

还是下面这个地方出错误?其他好像也没看到会出错的地方哦
for i:=0 to DBgrideh1.FieldCount-1 do
begin
items.add(DBGridEh1.Columns.Title.Caption);
end;
 
报错的信息是什么,你显示出来看看啊
 
个人意见:"这些中文字段添中到combobox1中,以便选择,用来查询"的代码我的写法是:
在mainform的onCreate或者onShow事件中写入.
adoQuery1:=TadoQuery.Create(self);
adoQuery1.Connection:=adoConnection1;
adoQuery1.close;
adoQuery1.SQL.Clear;
adoQuery1.SQL.Add('select * from workers');
adoQuery1.Open;
comboBox1.Items.Clear;
while not adoQuery1.Eof do
begin
comboBox1.Items.Add(adoQuery1.fieldByName('userID').AsString);
adoQuery1.Next;
end;
//comboBox1.SetFocus;
adoQuery1.Close;
之后,在"查询"按钮中执行select语句即可.
 
问题解决了。是字段名的问题。
no和position改一下名就好了,它们是sql的关键字。
笨笨来领分。
 
我来了,给分给分[:)][:)]
 
给了。接受答案了。
 
给了。接受答案了。
 
后退
顶部