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

想打开一个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查询器:

 

代码如下:

 

View Code
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[1in ['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);
  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;

 

 

   

 

posted @ 2011-06-28 16:12  步三少  阅读(2613)  评论(0编辑  收藏  举报