delphi使用BDE创建一个简易的dbf查询器(转)

很久就想用delphi操作dbf数据库文件

找到了一方法简单的代码

edtDir.text:=opendialog1.FileName;
dbdir:=ExtractFileDir(opendialog1.FileName);
dbname:=extractfilename(opendialog1.FileName);
dbname:=copy(dbname,1,(length(dbname)-4));//去掉.dbf, 不去也不会有影响

with table1 do
begin
Databasename:=dbdir;//设置库路径
TableName:=dbname;
Open;
end;

顺便做了一个简单的dbf查询器:

简单的dbf查询器

implementation

var

dbdir,dbname:string;

{$R *.dfm}

 

{思路:使用BDE驱动,共用一个DataSource,因此DS变动时,DBGrid以及

条件筛选的字段列表也会改变}

 

procedure TForm6.btnSetDirClick(Sender: TObject);

begin

 

if opendialog1.Execute then

begin

{初始化}

cmbfield.Clear;

edtvalue.Clear;

 

if table1.Active then

begin

table1.Active:=false;

if table1.Filtered then //必须取消filter,否则出错

table1.Filtered:=false;

table1.filter:=”;

end;

if query1.Active then

begin

query1.Active:=false;

query1.SQL.Clear;

end;

 

//datasource1.DataSet.Active:=false;

{初始化结束}

edtDir.text:=opendialog1.FileName;

dbdir:=ExtractFileDir(opendialog1.FileName);

dbname:=extractfilename(opendialog1.FileName);

dbname:=copy(dbname,1,(length(dbname)-4));

 

with table1 do

begin

Databasename:=dbdir;

TableName:=dbname;

Open;

datasource1.DataSet:=table1;

memo1.Lines.Clear;

if dbname[1] in [‘0’..’9′] then //如果数字开头,必须使用”

dbname:=quotedstr(dbname);

memo1.Lines.Add(‘select * from ‘+dbname);

end;//with

end; //if

 

end;

 

procedure TForm6.btnTjQryClick(Sender: TObject);

var

strtj:string;

begin

 

strtj:=cmbfield.Items[cmbfield.ItemIndex]+ ‘ ‘+cmbtj.Items[cmbtj.ItemIndex];

strtj:=strtj+ ‘ ‘+quotedstr(edtvalue.Text);

 

//showmessage(strtj);

with DataSource1.DataSet do

begin

if not active then

active:=true;

filter:=strtj;

filtered:=true;

btncancel.Enabled:=true;

end;

end;

 

{更新字段下拉列表;以及标签记录数}

procedure TForm6.DataSource1DataChange(Sender: TObject; Field: TField);

var

i:integer;

begin

 

i:=cmbfield.ItemIndex; //保留原itemindex

DataSource1.DataSet.GetFieldNames(cmbfield.Items);//http://www.onbno.com

cmbfield.ItemIndex:=i;

 

btnTjqry.Enabled:=true;

lblcount.Caption:=’ –共有’+inttostr(Datasource1.DataSet.recordcount)+’条记录’;

end;

 

procedure TForm6.btnCancelClick(Sender: TObject);

begin

with DataSource1.DataSet do

begin

if Filtered then

begin

Filtered:=false;

btnCancel.Enabled:=false;

end;

end;

end;

 

procedure TForm6.btnQryClick(Sender: TObject);

begin

with query1 do

begin

if table1.active then table1.active:=false;

 

if active then active:=false;

Databasename:=dbdir;

sql.Text:=memo1.Lines.Text;

open;

datasource1.DataSet:=query1;

btnTjQry.Enabled:=false;

 

end;

 

end;

简单的dbf查询器

 » 本站地址:http://www.gomoth.com

标签: