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
總結
以上是生活随笔為你收集整理的Delphi下物理删除dBase数据库的*.dbf文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hamcrest包含匹配器
- 下一篇: linux cmake编译源码,linu