使用AdoQuery 打开DBF文件后,删除DBF文件无法成功,请高手指点下。
测试代码如下:
procedure TForm1.Button1Click(Sender: TObject);
const
  DBFCONSTR='Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=%s;Exclusive=No';
var
  sFileName :String;
begin
  if dlgOpendlg.Execute then
  begin
    adoDvp.ConnectionString := Format(DBFCONSTR,[dlgOpendlg.FileName]);
    adoDvp.SQL.Text := 'select * from '+ExtractFileName(dlgOpendlg.FileName);
    adoDvp.Open;
    adoDvp.Close;
    if not DeleteFile(dlgOpendlg.FileName) then
      ShowMessage('无法删除!');
  end;
end;

解决方案 »

  1.   

    我的AdoQuery已经关闭了,没有使用AdoConnection。
    谢谢楼上兄弟
      

  2.   

    如果确保ado已经断开,那么就加一个延时试试,因为close的瞬间,不一定就完全断开
    另外是不是有其他ado与dbf相连,也就是多个地方连接了这个文件
      

  3.   

    没有其他控件与此DBF文件有关。就一个AdoQuery,我换成 AdoConnection+AdoQuery 来操作同样不可以删除。
    郁闷。必须退出程序才可以。
      

  4.   

    procedure TForm1.Button1Click(Sender: TObject);
    const
      DBFCONSTR='Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=%s;Exclusive=No';
    var
      adoq :TADOQuery;
    begin  if dlgOpendlg.Execute then
      try
        adoq :=TADOQuery.Create(nil);
        adoq.ConnectionString := Format(DBFCONSTR,[dlgOpendlg.filename]);
        adoq.SQL.Text := 'select * from '+ExtractFileName(dlgOpendlg.FileName);
        adoq.Open;
        adoq.Close;
        if not DeleteFile(dlgOpendlg.FileName) then
          ShowMessage('未删除');
      finally
        adoq.Close;
        adoq.Free;
      end;
    end;改成动态的也无法删除,那位大侠有经验的给指点下。
      

  5.   

    这样可以不?if dlgOpendlg.Execute then 
      try 
        adoq :=TADOQuery.Create(nil); 
        adoq.ConnectionString := Format(DBFCONSTR,[dlgOpendlg.filename]); 
        adoq.SQL.Text := 'select * from '+ExtractFileName(dlgOpendlg.FileName); 
        adoq.Open; 
        adoq.Close; 
        
      finally 
        adoq.Close; 
        adoq.Free; 
        if not DeleteFile(dlgOpendlg.FileName) then 
          ShowMessage('未删除'); 
      end; 
      

  6.   

    谢谢楼上的,现在发现的问题是程序目录下有一DBF文件,我想把它拷贝出去后再操作,结果提示我找不到 aa.dbf文件。
    代码如下:procedure TForm1.Button3Click(Sender: TObject);
    const
      DBFCONSTR='Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=%s;Exclusive=No';
    var
      adoq :TADOQuery;
      sFileName,sOrgName :String;
    begin
      adoq :=TADOQuery.Create(nil);
      try
        sOrgName :=ExtractFilePath(Application.ExeName)+'Tmp\model.dbf' ;
        sFileName :='C:\AA.dbf';

        CopyFile(PChar(sOrgName),PChar(sFileName),True);    adoq.ConnectionString := Format(DBFCONSTR,[sFileName]);
        adoq.SQL.Text := 'select * from '+ExtractFileName(sFileName);
        adoq.Open;
      finally
        adoq.Close;
        adoq.Free;
        if not DeleteFile(sFileName) then
          ShowMessage('未删除');
      end;
    end;
      

  7.   

    复制是成功的,C盘下确实有AA.dbf,但Open的时候报错的
      

  8.   

    试试在CopyFile(PChar(sOrgName),PChar(sFileName),True); 
    这句后面加个延时,sleep一段时间看看
      

  9.   

    即便是复制到程序所在目录的子目录中操作也提示找不到AA.dbf。procedure TForm1.Button3Click(Sender: TObject);
    const
      DBFCONSTR='Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=%s;Exclusive=No';
    var
      adoq :TADOQuery;
      sFileName,sOrgName :String;
    begin
      adoq :=TADOQuery.Create(nil);
      try
        sOrgName :=ExtractFilePath(Application.ExeName)+'Tmp\model.dbf' ; 
        //sFileName :='C:\AA.dbf';
        sFileName :=ExtractFilePath(Application.ExeName)+'OUT\AA.dbf';
        DeleteFile(sFileName);
        CopyFile(PChar(sOrgName),PChar(sFileName),True);
        adoq.ConnectionString := Format(DBFCONSTR,[sFileName]);
        adoq.SQL.Text := 'select * from '+ExtractFileName(sFileName);
        adoq.Open;
      finally
        adoq.Close;
        adoq.Free;
        if not DeleteFile(sFileName) then
          ShowMessage('未删除');
      end;end;
      

  10.   

    你的问题真是有点奇怪,没有用过FoxPro,无法测试
    要不你这样吧,在copyfile后面加一断点,看拷贝的文件用别的工具看是否能打开,然后确定问题出在哪里
      

  11.   

    解决了,应该就是SourceDb应该指定为DBF所在目录即可。  谢谢兄弟们的指导,结贴!
    procedure TForm1.Button3Click(Sender: TObject);
    const
      DBFCONSTR='Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=%s;Exclusive=No';
    var
      adoq :TADOQuery;
      sFileName,sOrgName :String;
    begin
      adoq :=TADOQuery.Create(nil);
      try
        sOrgName :=ExtractFilePath(Application.ExeName)+'Tmp\model.dbf' ;
        //sFileName :='C:\AA.dbf';
        sFileName :=ExtractFilePath(Application.ExeName)+'OUT\AA.dbf';
        DeleteFile(sFileName);
        CopyFile(PChar(sOrgName),PChar(sFileName),True);
        adoq.ConnectionString := Format(DBFCONSTR,[ExtractFilePath(Application.ExeName)+‘OUT']);
        adoq.SQL.Text := 'select * from '+ExtractFileName(sFileName);
        adoq.Open;
      finally
        adoq.Close;
        adoq.Free;
        if not DeleteFile(sFileName) then
          ShowMessage('未删除');
      end;end;