我是一名技校学生,目前正学习Delphi ,在学习实践中遇到一些问题。听网友说这儿的高手很多,待人热情,乐于助人,尤其关心我们这类新手的成长进步,故特来请教,恳请各位大侠伸出援助之手。
我参考相关大侠提供的信息而编写的程序如下,虽然编译通过,但运行生成的Project后系统总是提示数据备份没有成功!,数据恢复失败!(当然在指定目录下也查询不到备份的数据庫文件) 请问各位大侠这是怎么原因,如何修改?
//数据备份
SaveDialog1.Execute;
datamoduleado.ADOConnection1.Close;
if copyfile(pchar(getcurrentdir+'\long.mdb'),pchar(SaveDialog1.FileName+'.mdb'),False)
then ShowMessage('数据备份成功!文件路径为:'+SaveDialog1.FileName+'.mdb')
else showmessage('数据备份没有成功!');
datamoduleado.ADOConnection1.open;
//数据恢复
OpenDialog1.Execute;
if CopyFile(PChar(OpenDialog1.FileName),pchar(getcurrentdir+'\long.mdb'),False)
then begin
datamoduleado.ADOConnection1.Close;
datamoduleado.ADOConnection1.open;
ShowMessage('数据恢复成功!')
end
else showmessage('数据恢复失败!');
我参考相关大侠提供的信息而编写的程序如下,虽然编译通过,但运行生成的Project后系统总是提示数据备份没有成功!,数据恢复失败!(当然在指定目录下也查询不到备份的数据庫文件) 请问各位大侠这是怎么原因,如何修改?
//数据备份
SaveDialog1.Execute;
datamoduleado.ADOConnection1.Close;
if copyfile(pchar(getcurrentdir+'\long.mdb'),pchar(SaveDialog1.FileName+'.mdb'),False)
then ShowMessage('数据备份成功!文件路径为:'+SaveDialog1.FileName+'.mdb')
else showmessage('数据备份没有成功!');
datamoduleado.ADOConnection1.open;
//数据恢复
OpenDialog1.Execute;
if CopyFile(PChar(OpenDialog1.FileName),pchar(getcurrentdir+'\long.mdb'),False)
then begin
datamoduleado.ADOConnection1.Close;
datamoduleado.ADOConnection1.open;
ShowMessage('数据恢复成功!')
end
else showmessage('数据恢复失败!');
这分明是拷贝
即使是拷贝,也要杀死数据库进程的
备份数据库和恢复数据库用
BACKUP 和 RESTORE具体查阅SQL帮助文件
var
dao:OLEVariant;
Path : String;
begin
Data1.Connection1.Connected := False;
//Access数据库的路径
Path := ExtractFilePath(Application.Exename);
screen.Cursor := crHourGlass;
dao := CreateOleObject('DAO.DBEngine.36');
//压缩数据库
dao.CompactDatabase(Path + '\MyDataBase.mdb',Path + '\MyDataBase.tmp','',0);
DeleteFile(Path + '\MyDataBase.mdb');
RenameFile(Path + '\MyDataBase.tmp',Path + '\MyDataBase.mdb');
//备份数据库
if FileExists(Path + '\MyDataBase.mdb.bak') then
begin
DeleteFile(Path + '\MyDataBase.mdb.bak');
end;
CopyFile(Pchar(Path + '\MyDataBase.mdb'),Pchar(Path + '\MyDataBase.mdb.bak'),False);
screen.Cursor := crDefault;
end; procedure TMainForm.N16Click(Sender: TObject);
var AppPath :string;
Myini :TiniFile;
begin
try //备份数据
MyIni := TIniFile.Create('jeans.ini');
MyIni.WriteString('DataBack','BackTime',FormatDateTime('yyyy-mm-dd',Date));
AppPath := ExtractFilePath(Application.ExeName);
CopyFile(PChar(AppPath+'jeans.mdb'),PChar(AppPath+'backup.mdb'),false);
MainForm.PshowMessage('提示','数据已经成功备份!',0);
MyIni.Free;
except
MyIni.Free;
MainForm.PshowMessage('系统错误','数据备份失败!',0);
end;
end;
procedure TMainForm.N17Click(Sender: TObject);
var BackDate, AppPath:string;
MyIni :TIniFile;
begin
try//恢复数据
MyIni := TIniFile.Create('jeans.ini');
BackDate := MyIni.ReadString('DataBack','BackTime','');
Finally
MyIni.Free;
end; if BackDate <>'' then
begin
if MainForm.PshowMessage('提示','最近一次备份数据的日期是“'+BackDate+
'”。'+#13+#13+'如果恢复数据则这日之后的数据都会丢失,是否继续?',1) then
begin
try
MDataForm.DataADOConnection.Connected := false;
AppPath := ExtractFilePath(Application.ExeName);
CopyFile(PChar(AppPath+'backup.mdb'),PChar(AppPath+'jeans.mdb'),false);
MainForm.PshowMessage('提示','数据已经成功恢复!请查对数据!',0);
except
MainForm.PshowMessage('系统错误','恢复数据时失败!',0);
end;
end;
end else
MainForm.PshowMessage('提示','没有备份数据可以使用!',0);
end;
我今天晕的不行了
回答完这个问题下了,明天再来
郁闷这是一个移动、删除正在使用文件的例子,请参考
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
const
FILE_DELETE=1;
FILE_RENAME=2;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
RadioGroup1: TRadioGroup;
Edit1: TEdit;
Edit2: TEdit;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Edit2Change(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}Function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean;
var
WindowsDirs: array [0..MAX_PATH + 1] of Char;
lpDirSrc,lpDirDes: array [0..MAX_PATH + 1] of Char;
VerPlatForm: TOSVersionInfoA;
StrLstDelte: TStrings;
filename,s :String;
i:integer;
begin
Result := FALSE;
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
GetVersionEx(VerPlatForm);
if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
begin
SetLastError(ERROR_NOT_SUPPORTED);
Exit;
end
else if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
if flag=FILE_DELETE then
Result := MoveFileEx(PChar(lpFileNameToSrc), nil,
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT)
else if (flag=FILE_RENAME) then
Result := MoveFileEx(lpFileNameToSrc, lpFileNameToDes,
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);
end
else begin
StrLstDelte := TStringList.Create;
GetWindowsDirectory(WindowsDirs, MAX_PATH + 1);
filename:=WindowsDirs;
if filename[length(filename)]<>'\' then filename:=filename+'\';
filename:=filename+'wininit.ini';
if FileExists(filename) then
StrLstDelte.LoadFromFile(filename);
if StrLstDelte.IndexOf('[rename]') = -1 then
StrLstDelte.Add('[rename]');
GetShortPathName(lpFileNameToSrc, lpDirSrc, MAX_PATH + 1);
if fileexists(lpFileNameToDes) then
GetShortPathName(lpFileNameToDes, lpDirDes, MAX_PATH + 1)
else begin
s:=extractfilename(lpFileNameToDes);
i:=pos('.',s);
if (i=0) then
begin
if length(s)>8 then raise exception.create('不是有效的短文件名(8+3格式)!');
end
else begin
if (i-1>8)or(length(s)-i>3) then raise exception.create('不是有效的短文件名(8+3格式)!');
end;
strcopy(lpDirDes,lpFileNameToDes);
end;
if (flag=FILE_DELETE) then {删除}
StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, 'NUL='+string(lpDirSrc))
else if (flag=FILE_RENAME) then {改名}
StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, string(lpDirDes)+'='+string(lpDirSrc)); StrLstDelte.SaveToFile(filename);
Result := TRUE;
StrLstDelte.Free;
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
edit1.text:=OpenDialog1.FileName;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
if OpenDialog1.Execute then
edit2.text:=OpenDialog1.FileName;
end;procedure TForm1.Button1Click(Sender: TObject);
var
i:uint;
begin
if RadioGroup1.ItemIndex=0 then i:=FILE_DELETE
else i:=FILE_RENAME;
if edit1.text='' then raise exception.create('源文件为空!');
if (i=FILE_RENAME)and(edit2.text='') then raise exception.create('目标文件为空!');
if not DeleteRenameFileAfterBoot(pchar(edit1.text),pchar(edit2.text),i) then
showmessage('出错了')
else showmessage('ok');
end;procedure TForm1.Edit2Change(Sender: TObject);
var
VerPlatForm: TOSVersionInfoA;
buf: array [0..MAX_PATH + 1] of Char;
begin
if not fileexists(edit2.text) then exit;
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
GetVersionEx(VerPlatForm);
if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
begin
GetShortPathName(pchar(edit2.text), buf, MAX_PATH + 1);
edit2.text:=buf;
end;
end;procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
edit2.Enabled:=RadioGroup1.ItemIndex=1;
button2.Enabled:=RadioGroup1.ItemIndex=1;
end;end.
怎么备份啊?