Delphi语言,无论Delphi7、Delphi2007或者Delphi XE2或3,无需安装其它附加的部件,就可以实现DBF文件的打开及相关操作,网络上很多要用到什么ADO引擎的,其实未必,只有安装的Delphi有BDE即可。
实例操作为例:
1、新添一个Tform,在上面添加Table、Datasource和Datagrid等组件,见下图:
2、设置Table1的DatabaseName属性为dBase files,Datasource1的DataSet属性为Table1,DataGrid1的DataSource为DataSource1
3、在Button1的单击事件中添加如下代码:
procedure TForm1.Button1Click(Sender: TObject); var filenm,path:string; begin if self.OpenDialog1.Execute then begin filenm:=extractfilename(self.OpenDialog1.filename); path:=extractfiledir(self.OpenDialog1.filename); self.Table1.Active:=false; self.Table1.databasename:=path; self.Table1.tablename:=filenm ; self.Table1.Active:=true; end; end;
即可实现dbf数据文件的打开
4、整个程序的代码如下:
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids, Data.DB, Bde.DBTables; type TForm1 = class(TForm) Table1: TTable; DataSource1: TDataSource; DBGrid1: TDBGrid; Button1: TButton; OpenDialog1: TOpenDialog; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var filenm,path:string; begin if self.OpenDialog1.Execute then begin filenm:=extractfilename(self.OpenDialog1.filename); path:=extractfiledir(self.OpenDialog1.filename); self.Table1.Active:=false; self.Table1.databasename:=path; self.Table1.tablename:=filenm ; self.Table1.Active:=true; end; end; end.
以上代码在Delphi XE3和XE4下运行通过,可以打开dbf文件,并自动在DataGrid1中显示所有数据。