如何备份和恢复ACCESS数据库(不是直接复制哦)
大侠们,救命啊!
小弟近期在用DELPHI开发一个数据库系统,用ACCESS。为了不让使用者接触到数据库,我想增加一个数据库备份和恢复的功能,但是BACKUP和RESTOR对ACCESS数据库没有作用,直接COPY的话肯定要接触到数据库,不知道各位有什么好的办法,SHARE一下!
[解决办法]
////数据备份
var S : string;
begin
if Application.MessageBox(Pchar( '确实要备份数据吗? '),
'询问 ',MB_YESNO+MB_ICONQUESTION) = IDNO then Abort;
if SaveDialog1.Execute then//此处还用了一个保存对话框
begin //Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=zsd;Initial Catalog=ybserver;Data Source=USER02
S := ADOCommand1.Connection.ConnectionString;
S := Copy(S, pos( 'Initial Catalog= ', S)+16, maxint);
S := Copy(S, 1, pos( '; ', S)-1);
label1.caption:= '正在备份数据,请稍后。。。。。 ';
try
ADOCommand1.CommandText := 'backup database '+S+ ' to disk = ' ' '+SaveDialog1.filename+ ' ' ' ' ;//with Init
ADOCommand1.Execute;
showmessage( '备份完成! ');
label1.caption:= '数据备份完成,请检查。 ';
except
On e:exception do
begin
showmessage( '备份失败! '#13#10+E.Message);
label1.caption:= '数据备份失败,请检查。 ';
end;
end;
end;
////数据还原
var S : string;
begin
if Application.MessageBox(Pchar( '确实要恢复数据吗? '),
'询问 ',MB_YESNO+MB_ICONQUESTION) = IDNO then Abort;
if OpenDialog1.Execute then//此处还用了一个保存对话框
begin //Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=zsd;Initial Catalog=ybserver;Data Source=USER02
S := ADOCommand1.Connection.ConnectionString;
S := Copy(S, pos( 'Initial Catalog= ', S)+16, maxint);
S := Copy(S, 1, pos( '; ', S)-1);
label1.caption:= '正在恢复数据,请稍后。。。。。 ';
try
ADOCommand1.CommandText := 'restore database '+S+ ' from disk = ' ' '+SaveDialog1.filename+ ' ' ' ' ;//with Init
ADOCommand1.Execute;
showmessage( '恢复完成! ');
label1.caption:= '数据恢复完成,请检查。 ';
except
On e:exception do
begin
showmessage( '恢复失败! '#13#10+E.Message);
label1.caption:= '数据恢复失败,请检查。 ';
end;
end;
end;
这两个语句在DELPHI+SQL系统中调试通过!
[解决办法]
//BACK UP
function BackupDatabase(TargetFileName: String): Boolean;
var
A1, A2, TargetFile: TFileStream;
myFileSize: Integer;
begin
try
A1 := TFileStream.Create(MainDM.CurrentSysEnv.SysDirectory + '\1.mdb ',
fmOpenRead or fmShareDenyNone);
A2 := TFileStream.Create(MainDM.CurrentSysEnv.SysDirectory + '\2.mdb ',
fmOpenRead or fmShareDenyNone);
TargetFile := TFileStream.Create(TargetFileName,fmCreate);
myFileSize := A1.Size;
TargetFile.CopyFrom(A1, A1.Size);
TargetFile.CopyFrom(A2, A2.Size);
TargetFile.WriteBuffer(MyFileSize, SizeOf(MyFileSize));
finally
A1.Free;
A2.Free;
TargetFile.Free;
end;
end;
//RESTORE
function RestoreDatabase(SourceFileName: String): Boolean;
var
A1, A2, SourceFile: TFileStream;
myFileSize: Integer;
begin
try
ACSFile := TFileStream.Create(MainDM.CurrentSysEnv.SysDirectory + '\1.mdb ',
fmCreate);
ACTFile := TFileStream.Create(MainDM.CurrentSysEnv.SysDirectory + '\2.mdb ',
fmCreate);
SourceFile := TFileStream.Create(SourceFileName, fmOpenRead or fmShareDenyNone);
SourceFile.Seek(-SizeOf(MyFileSize), soFromEnd);
SourceFile.ReadBuffer(MyFileSize,sizeof(MyFileSize));
SourceFile.Seek(0, soFromBeginning);
A1.CopyFrom(SourceFile, MyFileSize);
SourceFile.Seek(MyFileSize, soFromBeginning);
A2.CopyFrom(SourceFile, SourceFile.Size-MyFileSize-SizeOf(MyFileSize));
finally
A1.Free;
A2.Free;
SourceFile.Free;
end;
end;
经常用.放心吧!~~~~~