ITPub博客

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

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

作者:gaokeke123 时间:2022-03-23 15:19:45 0 删除 编辑

{ ************************************************************ }
{                                                                                                              }
{ 数据库操作实例                                                                                          }
{                                                                                                              }
{ 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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26692020/viewspace-3148216/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: DRDS的基础原理
下一篇: RRDTool 详解
请登录后发表评论 登录
全部评论
大家好

注册时间:2013-08-14

  • 博文量
    1432
  • 访问量
    31652