我以前写的: sBakFileName := '''' + 'c:\' + FormatDateTime('yyyy-mm-dd',Now) + '.bak' + ''''; sBackupPath := 'BACKUP DATABASE ' + '[BankData] TO DISK=' + sBakFileName + ' WITH INIT,NAME = N' + '''' + 'DB备份' + ''''; with DM1.QTemp do begin close; sql.Clear; sql.Add(sBackupPath); ExecSQL; end;
(8).备份和恢复 procedure TForm1.Button1Click(Sender: TObject); begin if OpenDialog1.Execute then begin try adoconnection1.Connected:=False; adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=FRIEND-YOFZKSCO;'+ 'Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=FRIEND-YOFZKSCO;Use Encryption for Data=False;Tag with column collation when possible=False'; adoconnection1.Connected:=True; with adoQuery1 do begin Close; SQL.Clear; SQL.Add('Backup DataBase sfa to disk ='''+opendialog1.FileName+''''); ExecSQL; end; except ShowMessage('±?·Y꧰ü'); Exit; end; end; Application.MessageBox('1§?2?ú£?êy?Y±?·Y3é1|','ìáê?',MB_OK + MB_ICONINFORMATION); end; procedure TForm1.Button2Click(Sender: TObject); begin if OpenDialog1.Execute then begin try adoconnection1.Connected:=false; adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=FRIEND-YOFZKSCO;'+ 'Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=FRIEND-YOFZKSCO;Use Encryption for Data=False;Tag with column collation when possible=False'; adoconnection1.Connected:=true; with adoQuery1 do begin Close; SQL.Clear; SQL.Add('Restore DataBase sfa from disk ='''+opendialog1.FileName+''''); ExecSQL; end; except ShowMessage('???′꧰ü'); Exit; end; end; Application.MessageBox('1§?2?ú£?êy?Y???′3é1|','ìáê?',MB_OK + MB_ICONINFORMATION); end;
看看以前的帖子吧
你可以
1 直接文件copy
2 用back/restore database命令
3 将back/restore database做成存储过程或者job然后调用
sBakFileName := '''' + 'c:\' + FormatDateTime('yyyy-mm-dd',Now) + '.bak' + '''';
sBackupPath := 'BACKUP DATABASE ' + '[BankData] TO DISK=' + sBakFileName
+ ' WITH INIT,NAME = N' + '''' + 'DB备份' + '''';
with DM1.QTemp do
begin
close;
sql.Clear;
sql.Add(sBackupPath);
ExecSQL;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
try
adoconnection1.Connected:=False;
adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=FRIEND-YOFZKSCO;'+
'Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=FRIEND-YOFZKSCO;Use Encryption for Data=False;Tag with column collation when possible=False';
adoconnection1.Connected:=True;
with adoQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('Backup DataBase sfa to disk ='''+opendialog1.FileName+'''');
ExecSQL;
end;
except
ShowMessage('±?·Y꧰ü');
Exit;
end;
end;
Application.MessageBox('1§?2?ú£?êy?Y±?·Y3é1|','ìáê?',MB_OK + MB_ICONINFORMATION);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
try
adoconnection1.Connected:=false;
adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=FRIEND-YOFZKSCO;'+
'Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=FRIEND-YOFZKSCO;Use Encryption for Data=False;Tag with column collation when possible=False';
adoconnection1.Connected:=true;
with adoQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('Restore DataBase sfa from disk ='''+opendialog1.FileName+'''');
ExecSQL;
end;
except
ShowMessage('???′꧰ü');
Exit;
end;
end;
Application.MessageBox('1§?2?ú£?êy?Y???′3é1|','ìáê?',MB_OK + MB_ICONINFORMATION);
end;