Delphi中ADO控件删除dbf数据库
处理一dbf数据库,要求每次保存数据前删除dbf中现有记录,我知道BDE可以实现,但因为我的程序主数据库是SQL Server的,所以采用ADO,在网上查过,dbf数据库文件对记录的删除是软删除,在foxpro中要调用Pack才能实现物理删除,有没有哪位有解决方案,非常感激。
[解决办法]
方法比較笨,不過實現了dbf的物理刪除...
procedure TForm1.Button1Click(Sender: TObject);var adoconnection1,adoconnection2:Tadoconnection;begin adoconnection1:=Tadoconnection.Create(nil); adoconnection1.ConnectionString:='Provider=MSDASQL.1;Password="";Persist Security Info=True;Data Source=dbf1'; adoconnection1.Open; adoconnection1.execute('delete from X1-31-P40'); adoconnection1.Close; adoconnection1.Free; //adoconnection1.Destroy; adoconnection2:=Tadoconnection.Create(nil); adoconnection2.ConnectionString:='Provider=MSDASQL.1;Password="";Persist Security Info=True;Data Source=dbf1';//dbf1是指向自由表文件的odbc数据源 adoconnection2.Open; adoconnection2.execute('pack X1-31-P40.dbf'); adoconnection2.Close; adoconnection2.Free; //adoconnection2.Destroy; end;
[解决办法]
如果是軟刪除,又要恢復data,那麼可以這樣...
在Delphi應用程式中,對DBF資料表執行的刪除操作為軟刪除操作.由於物理記錄並沒 有從資料表中刪除,我們就可以恢復被軟刪除的記錄,只要去掉刪除標誌即可.
要用到的函數名為DbiUndeleteRecord,函數原型為:
functionDbiUndeleteRecord (hCursor:hDBICur):DBIResult;其中,hCursor可為資料集的Handle . 下面就是一個典型的例子. 同樣,應在uses語句加上:DbiTypes,DbiProcs,DbiErrs. {恢復被軟刪除的記錄}functionUndeleteRecord(DbfTable:TTable):boolean;beginResult:=false;{返回false表示函數執行失敗}withDbfTabledobegin{如果資料集處於非活動狀態,則執行失敗}ifnotactivethenexit;{使資料集的目前記錄與實際的目前記錄的位置一致}UpdateCursorPos;{恢復被軟刪除的記錄}ifDBIERR_NONEDbiUndeleteRecord(Handle)thenexit;{發生錯誤,操作失敗}result:=true; {操作成功}end;end;
[解决办法]
ocommand1.commandtext:='pack X1-31-P40.dbf';
adocommand1.Execute;