自动附加Sqlserver数据库

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;
posted @ 2013-01-06 15:53  骑着骆驼逛沙漠  阅读(169)  评论(0编辑  收藏  举报