function TForm1.ReadSqlPath:string;
var
Reg: TRegistry;
begin
Result:='';
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
try
//从注册表中取参数
if Reg.OpenKey('/Software/microsoft/mssqlserver/setup',True) then
begin
Result:=Reg.ReadString('sqlpath');
end;
except
//取参数出错
Result:='';
end;
finally
Reg.CloseKey;
Reg.Free;
end;
end;
下面用到的strSqlPath是上面函数的返回值
procedure TForm1.RestoreDatabase(a_edt:TEdit;a_strDatabase:string);
var
bError:boolean;
strDataName,
strLogName:string;
begin
bError:=False;
AddLog(Log,'开始处理'+a_strDatabase+'任务',clBlack);
if FileExists(a_edt.text) then
begin
AddLog(Log,a_strDatabase+'数据库文件存在,开始取文件信息',clBlack);
end
else
begin
AddLog(Log,a_strDatabase+'数据库文件不存在,请重新选择',clRed);
bError:=True;
end;
if not bError then
begin
Query.Close;
Query.SQL.Clear;
Query.SQL.Add('restore FileListOnly from disk='+
QuotedStr(a_edt.text));
try
Query.Open;
except
bError:=True;
AddLog(Log,'恢复'+a_strDatabase+'数据库失败,请检查文件是否正确,再重试',clRed);
Application.ProcessMessages;
end;
if not bError then
begin
strDataName:=Query.FieldByName('LogicalName').asstring;
Query.Next;
strLogName:=Query.FieldByName('LogicalName').asstring;
AddLog(Log,'取文件信息成功,开始恢复数据库,请稍候。。。',clBlack);
Application.ProcessMessages;
Query.Close;
Query.SQL.Clear;
Query.SQL.Add('restore database '+a_strDatabase+' from disk='+
QuotedStr(a_edt.text)+
' with move '+QuotedStr(strDataName)+
' to '+Quotedstr(strSqlPath+'/data/'+a_strDatabase+'.mdf')+
',move '+QuotedStr(strLogName)+
' to '+QuotedStr(strSqlPath+'/data/'+a_strDatabase+'.ldf')+','+
'replace');
try
Query.ExecSQL;
except
bError:=True;
AddLog(Log,'恢复'+a_strDatabase+'数据库失败,请检查文件是否正确,再重试',clRed);
Application.ProcessMessages;
end;
end;
if Not bError then
AddLog(Log,'恢复'+a_strDatabase+'数据库成功',clBlue);
Application.ProcessMessages;
end;
AddLog(Log,'********************',clGreen);
Application.ProcessMessages;
end;