Delphi下物理删除dBase数据库的*.dbf文件
{ ************************************************************ }
{ }
{ 数据库操作实例 }
{ }
{ Date:2012-12-13 Author:Phantom0917 Addr:NJ Modify:2012-12-14 }
{ }
{ ************************************************************ }
{ ************************************************************* }
{ 调用BDE函数DbiPackTable物理删除dBase的*.dbf格式的数据 }
{ 使用DbiPackTable函数前在uses中引入Dbi*,DbiErrs,DbiProcs }
{ ************************************************************* }
unit untDelVFP;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBTables,Dbi*,DbiErrs,DbiProcs, ADODB;
type
TfrmDelVFP = class(TForm)
btnDeldBase: TButton;
DBaseTable: TTable;
ADOQuery1: TADOQuery;
procedure btnDeldBaseClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmDelVFP: TfrmDelVFP;
DBConnVFP: TADOConnection = nil;
implementation
{$R *.dfm}
procedure TfrmDelVFP.btnDeldBaseClick(Sender: TObject);
var
Error: DbiResult;
ErrorMsg: String;
Special: DBIMSG;
begin
{**通过ADO方式连接dBase数据库,使用SQL语句delete软删除*.dbf数据**}
{**打开*.dbf文件会发现数据已经“清空”,但文件大小还是和删除前一样,没有变化**}
{**因为通过SQL语句去删除,只是做了一个删除标记,并没有物理删除数据**}
if DBConnVFP = nil then
DBConnVFP := TADOConnection.Create(nil);
if DBConnVFP.Connected <> True then
begin
DBConnVFP.ConnectionString:='Provider=MSDASQL.1;Persist Security Info=False;Data Source=dBASE Files;Initial Catalog='+extractfilepath('');
DBConnVFP.KeepConnection := True;
DBConnVFP.LoginPrompt := False;
try
DBConnVFP.Connected := True;
except
showmessage('数据库连接失败!');
Exit;
end;
end;
ADOQuery1.Close;
ADOQuery1.Connection := DBConnVFP;
ADOQuery1.SQL.Text := 'delete from cbq';
ADOQuery1.ExecSQL;
{**通过BDE的DbiPackTable函数进行物理删除**}
{**使用前要在uses中加入Dbi*,DbiErrs,DbiProcs**}
//确定要删除表的位置
DBaseTable.Active:=False;
DBaseTable.DatabaseName := extractfilepath('');
DBaseTable.TableName :='cbq.DBF';
DBaseTable.Active:=true;
DBaseTable.Close;
//调用BDE的DbiPackTable函数
try
DBaseTable.Exclusive := True;
DBaseTable.Active := True;
Error := DbiPackTable(DBaseTable.DBHandle, DBaseTable.Handle, nil, szdBASE, True);
DBaseTable.Active := False;
DBaseTable.Exclusive := False;
finally
DBaseTable.Active := True;
end;
case Error of
DBIERR_NONE:
ErrorMsg := 'Successful';
DBIERR_INVALIDPARAM:
ErrorMsg := 'The specified table name or the pointer to the table name is NULL';
DBIERR_INVALIDHNDL:
ErrorMsg := 'The specified database handle or cursor handle is invalid or NULL';
DBIERR_NOSUCHTABLE:
ErrorMsg := 'Table name does not exist';
DBIERR_UNKNOWNTBLTYPE:
ErrorMsg := 'Table type is unknown';
DBIERR_NEEDEXCLACCESS:
ErrorMsg := 'The table is not open in exclusive mode';
else
DbiGetErrorString(Error, Special);
ErrorMsg := '[' + IntToStr(Error) + ']: ' + Special;
end;
{**************************************}
{*****物理删除结束后要把Table关闭******}
{**如果不关闭,程序不退出,直接打开.dbf文件会提示**}
{**文件正在使用,‘**.dbf正处于锁定状态,“另一用户”正在编辑。。**}
DBaseTable.Active := False;
DBaseTable.Exclusive := False;
MessageDlg(ErrorMsg, mtInformation, [mbOk], 0);
end;
end.
相关文章