我的应用流程是这样的:
我在SQLServer中建了两个数据库,一个是主库info,一个是备份库bkinfo;
备份库用来存放备份信息。
我把主库中的记录导出到文件中(我用的是Backup语句),但是我用restore语句从该文件导入到备份库的时候,报“无法重写info.mdf文件,数据库info正在使用该文件”
我感到很奇怪,我要将文件恢复到bkinfo啊,它怎么报无法重写info了,又没有要它恢复到info库中,望各位老大指点啊在线等,解决了马上结贴!!!!

解决方案 »

  1.   

    : zjcxc(邹建) ( ) 信誉:316 
    /*
    关闭用户打开的进程处理
    */
    use master
    goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[p_killspid]
    GOcreate proc p_killspid
    @dbname varchar(200) --要关闭进程的数据库名
    as  
    declare @sql  nvarchar(500)  
    declare @spid nvarchar(20) declare #tb cursor for
    select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
    open #tb
    fetch next from #tb into @spid
    while @@fetch_status=0
    begin  
    exec('kill '+@spid)
    fetch next from #tb into @spid
    end  
    close #tb
    deallocate #tb
    go--用法  
    exec p_killspid  '数据库名'
    =======================================================
    用的delphi执行上面的脚本之后,在使用restore命令恢复
      

  2.   

    我觉得是不是我导出整个数据库,其文件中记录了数据库名,我再导入到其他的数据库中,这样子造成了冲突?zjcxc(邹建) ,你的脚本怎么在delphi中执行啊?
    能否给出delphi代码?
      

  3.   

    unit BackUp_Form;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, TFlatGaugeUnit, StdCtrls, TFlatEditUnit, ExtCtrls,FileCtrl;type
      TBackup_F = class(TForm)
        Label1: TLabel;
        Label2: TLabel;
        ZT_FE: TFlatEdit;
        JD_FG: TFlatGauge;
        Timer1: TTimer;
        procedure Timer1Timer(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Backup_F: TBackup_F;implementation{$R *.dfm}Uses
      ExtIniFiles,StrUtils;function GetFileNum(dir:string):integer;
    var
      sr:Tsearchrec;
    begin
      result:=0;
      if findfirst(dir,faanyfile,sr)=0 then
        repeat
          if(sr.Attr<>fadirectory)and(sr.Name<>'.')and(sr.Name<>'..') then
            result:=result+1;
        until findnext(sr)<>0;
    end;
    //***********************************************************************************
    procedure Wait(second:Longint);
    var
      FirstTickCount:longint;
    begin
      FirstTickCount:=GetTickCount;
      repeat
        Application.ProcessMessages;
      until ((GetTickCount-FirstTickCount) >=second);
    end;
    //***********************************************************************************
    procedure TBackup_F.Timer1Timer(Sender: TObject);
    var
      myini:TExtIniFile;
      cz,num:integer;
      spath,dpath,name,RQ:string;
      sr:Tsearchrec;
      StartupInfo:TStartupInfo;
      ProcessInfo:TProcessInformation;
    begin
      Timer1.Enabled:=false;
      myini:=TExtIniFile.Create(ExtractFilePath(Application.ExeName)+'set.ini');
      cz:=myini.ReadInteger('bakup','CZ',0);
      if cz=0 then
        begin
          spath:=ExtractFilePath(Application.ExeName)+'Data';
          dpath:=myini.ReadString('bakup','BakPath','');
          dpath:=dpath+'\'+AnsiReplaceStr(DateTimeToStr(now),':','');
          ForceDirectories(dpath);
          num:=GetFileNum(spath+'\*.*');
          ZT_FE.Text:='正在备份数据库......';
          JD_FG.MaxValue:=num;
          if findfirst(spath+'\*.*',faanyfile,sr)=0 then
            repeat
              if(sr.Attr<>fadirectory)and(sr.Name<>'.')and(sr.Name<>'..') then
                begin
                  copyfile(pchar(spath+'\'+sr.Name),pchar(dpath+'\'+sr.Name),false);
                  JD_FG.Progress:=JD_FG.Progress+1;
                end;
              Wait(100);
            until findnext(sr)<>0;
          ZT_FE.Text:='数据库备份成功';
          MessageBox(handle,'恭喜你:数据库备份成功!','祝贺你',MB_OK or MB_ICONINFORMATION);
        end
        else begin
          RQ:=myini.ReadString('bakup','RQ','');
          spath:=myini.ReadString('bakupdian',RQ,'');
          spath:=spath+'\'+RQ;
          dpath:=ExtractFilePath(Application.ExeName);
          dpath:=dpath+'Data';
          num:=GetFileNum(spath+'\*.*');
          ZT_FE.Text:='正在还原数据库......';
          JD_FG.MaxValue:=num;
          if findfirst(spath+'\*.*',faanyfile,sr)=0 then
            repeat
              if(sr.Attr<>fadirectory)and(sr.Name<>'.')and(sr.Name<>'..') then
                begin
                  copyfile(pchar(spath+'\'+sr.Name),pchar(dpath+'\'+sr.Name),false);
                  JD_FG.Progress:=JD_FG.Progress+1;
                end;
              Wait(100);  
            until findnext(sr)<>0;
          ZT_FE.Text:='数据库还原成功';
          MessageBox(handle,'恭喜你:数据库还原成功!','祝贺你',MB_OK or MB_ICONINFORMATION);
        end;
      StartupInfo.cb:=sizeof(StartupInfo);
      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      CreateProcess(pchar(ExtractFilePath(Application.ExeName)+'TSGL.exe'),nil,nil,nil,false,0,nil,nil,StartupInfo,ProcessInfo);
      close;
    end;end.
      

  4.   


    create proc p_killspid
    @dbname varchar(200) --要关闭进程的数据库名
    as  
    declare @sql  nvarchar(500)  
    declare @spid nvarchar(20) declare #tb cursor for
    select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
    open #tb
    fetch next from #tb into @spid
    while @@fetch_status=0
    begin  
    exec('kill '+@spid)
    fetch next from #tb into @spid
    end  
    close #tb
    deallocate #tb
    在你的数据库当中运行---------生成一个过程delphi中
    query.sql.add('use master');
    query.sql.add(' p_killspid '+你的数据库名');
    query.sql.add('restore database...')
      

  5.   

    谢谢jinjazz(我是jin)。我试试先
    我也觉得海龙的好像是备份access之类的啊
    呵呵
      

  6.   

    jinjazz(我是jin) 
    我的数据库是程序创建的,可以把存储过程在delphi中写了然后送到数据库中去吗?
    因为不可能人工写啊。再次感谢
      

  7.   

    qry.sql.loatfromfile('.\xxx.sql');
    qry.execsql;
    或者用
    winexec('-isql -S. -Usa -P -i .....')的命令执行脚本
      

  8.   

    结贴,jinjazz(我是jin)来领分,还有一些问题想向你请教,我的qq号是7903527,加我聊聊吧
      

  9.   

    我的代码是用来备份SQL数据库的呀