自动附加Sqlserver数据库

来源:互联网 发布:华为守家多个软件 编辑:程序博客网 时间:2024/05/01 17:59
procedure TfrmMain.RestoreDataBase;
var
  CmdStr,
  Paths:String;
begin
  Paths:=Extractfilepath(Application.ExeName);
  if fileexists(ConCat(Paths,'数据库文件名称')) then
    try
    CmdStr:='ISQL -E -Q "if (select Count(*) from sysdatabases where name=%s)=0 EXEC sp_attach_db @dbname = N%s,@filename1 = N%s,@filename2 = N%s"';
    CmdStr:=format(CmdStr,[ConCat(#39,'NGISdbms',#39),
                           ConCat(#39,'数据库名称',#39),
                           ConCat(#39,Paths,'数据库文件名称',#39),
                           ConCat(#39,Paths,'数据库日志文件名称',#39)]);
    WinExec(PChar(CmdStr),0);
    except
    on E:Exception do
      Logmemo.Add(e.Message);
    end;
end;
原创粉丝点击