使用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;
测试代码如下:
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;
谢谢楼上兄弟
另外是不是有其他ado与dbf相连,也就是多个地方连接了这个文件
郁闷。必须退出程序才可以。
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;改成动态的也无法删除,那位大侠有经验的给指点下。
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;
代码如下: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;
这句后面加个延时,sleep一段时间看看
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;
要不你这样吧,在copyfile后面加一断点,看拷贝的文件用别的工具看是否能打开,然后确定问题出在哪里
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;