procedure TForm1.Button5Click(Sender: TObject);//还原之前必须先得到逻辑文件名
var sqlstr :string;
begin
ADOQuery1.ConnectionString :='Provider=SQLOLEDB.1;Password='+EDIT3.TEXT+';Persist Security Info=True;User ID='+EDIT2.TEXT+';Initial Catalog=master;Data Source='+Edit1.TEXT+'';//连接数据库
with ADOQuery1 do
begin
if active then close;
sql.clear;
sqlstr :=' RESTORE FILELISTONLY FROM DISK = '+QuotedStr(Edit13.text) ;//得到逻辑文件名
sql.Add(sqlstr);
open;
first;
Edit12.text:= fields[0].asstring;
next;
Edit14.text := fields[0].asstring ;
close;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);//还原成新的数据库
var sqlstr :string;
begin
sqlstr :='RESTORE DATABASE '+Edit10.text;
sqlstr :=sqlstr+' FROM DISK = '+QuotedStr(Edit13.text);//备份文件位置
sqlstr :=sqlstr+' WITH MOVE '+QuotedStr(Edit12.text)+' TO '+QuotedStr(Edit11.text)+',';//mdf逻辑文件名Edit12.text,新的mdf存放位置Edit11.text
sqlstr :=sqlstr+' MOVE '+QuotedStr(Edit14.text)+' TO '+QuotedStr(Edit15.text);//log逻辑文件名Edit14.text,新的log存放位置Edit12.text
ADOCommand1.ConnectionString:='Provider=SQLOLEDB.1;Password='+EDIT3.TEXT+';Persist Security Info=True;User ID='+EDIT2.TEXT+';Initial Catalog=master;Data Source='+Edit1.TEXT+'';
ADOCommand1.CommandText:=sqlstr;
ADOCommand1.Execute;
showmessage('执行成功');
end;