Delphi下物理删除dBase数据库的*.dbf文件

{ ************************************************************ }
{                                                                                                              }
{ 数据库操作实例                                                                                          }
{                                                                                                              }
{ Date:2012-12-13    Author:Phantom0917    Addr:NJ   Modify:2012-12-14    }
{                                                                                                              }
{ ************************************************************ }

{ ************************************************************* }
{      调用BDE函数DbiPackTable物理删除dBase的*.dbf格式的数据              }
{      使用DbiPackTable函数前在uses中引入DbiTypes,DbiErrs,DbiProcs        }
{ ************************************************************* }

unit untDelVFP;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, DBTables,DbiTypes,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中加入DbiTypes,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.

转载于:https://www.cnblogs.com/phantom0917/archive/2012/12/14/2817539.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值