多谢j9988,
加: SQL.Add(' exec sp_dropdevice ''Lower_bak''');
后解决backup问题,但restore时的数据库占用怎样解决

解决方案 »

  1.   

    unit w_sjhf;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Db, DBTables, ComCtrls, StdCtrls, Buttons,registry;type
      Tsjhf = class(TForm)
        OpenDialog1: TOpenDialog;
        StoredProc1: TStoredProc;
        Database1: TDatabase;
        GroupBox1: TGroupBox;
        Label1: TLabel;
        Edit1: TEdit;
        BitBtn1: TBitBtn;
        StoredProc2: TStoredProc;
        BitBtn3: TBitBtn;
        BitBtn4: TBitBtn;
        SpeedButton1: TSpeedButton;
        procedure BitBtn1Click(Sender: TObject);
        procedure BitBtn2Click(Sender: TObject);
        procedure BitBtn3Click(Sender: TObject);
        procedure SpeedButton1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      sjhf: Tsjhf;implementation{$R *.DFM}procedure Tsjhf.BitBtn1Click(Sender: TObject);
    begin
       bitbtn3.Enabled:=false;
       if edit1.Text='' then
       begin
          application.messagebox('请选择备份文件名!','提示',mb_ok+mb_iconinformation);
          exit;
       end;
       with storedproc1 do
       begin
          close;
          parambyname('@path').asstring:=edit1.text;
          execproc;
          application.messagebox('数据已成功备份!','提示',mb_ok+mb_iconinformation);
       end;
       edit1.Text:='';
       bitbtn3.Enabled:=true;
    end;procedure Tsjhf.BitBtn2Click(Sender: TObject);
    begin
       close;
    end;procedure Tsjhf.BitBtn3Click(Sender: TObject);
    begin
       bitbtn1.Enabled:=false;
       if edit1.Text='' then
       begin
          application.messagebox('请选择恢复文件名!','提示',mb_ok+mb_iconinformation);
          exit;
       end;
       with storedproc2 do
       begin
          close;
          parambyname('@databasename').asstring:=edit1.text;
          execproc;
          application.messagebox('数据已成功恢复!','提示',mb_ok+mb_iconinformation);
       end;
       edit1.Text:='';
       bitbtn1.Enabled:=true;
    end;procedure Tsjhf.SpeedButton1Click(Sender: TObject);
    begin
       if bitbtn3.Enabled=false then
       begin
          OpenDialog1.Title:='备份数据:';
          if (OpenDialog1.Execute) then
             Edit1.Text:=OpenDialog1.FileName
          else
             bitbtn3.Enabled:=true;
       end;
       if bitbtn1.Enabled=false then
       begin
          OpenDialog1.Title:='恢复数据:';
          if (OpenDialog1.Execute) then
             Edit1.Text:=OpenDialog1.FileName
          else
             bitbtn1.Enabled:=true;
       end;
    end;procedure Tsjhf.FormCreate(Sender: TObject);
    var
       reg:tregistry;
    begin
       reg:=tregistry.Create;
       with reg do
       begin
          try
            rootkey:=HKEY_CURRENT_USER;
            if openkey('\Software\ODBC\ODBC.INI\sjbfhf',true) then
            begin
               writestring('Datebase','sjbfhf');
               writestring('Driver','C:\WINNT\System32\sqlsrv32.dll');
               writestring('LastUser','sa');
               writestring('Server','(local)');
            end;
            if openkey('\Software\ODBC\ODBC.INI\ODBC Data Sources',true) then
            begin
               writestring('sjbfhf','SQL Server');
            end;
          except
            closekey;
            free;
          end;
       end;
    end;end.
    不会是存储过程也写上吧:)
      

  2.   

    这个我前面曾经写过一个KILL SPID 的过程:
    create  proc  killspid  (@dbname  varchar(20))  
    as  
    begin  
    declare  @sql  nvarchar(500)  
    declare  @spid  int  
    set  @sql='declare  getspid  cursor  for    
    select  spid  from  sysprocesses  where  dbid=db_id('''+@dbname+''')'  
    exec  (@sql)  
    open  getspid  
    fetch  next  from  getspid  into  @spid  
    while  @@fetch_status  <  >-1  
    begin  
    exec('kill  '+@spid)  
    fetch  next  from  getspid  into  @spid  
    end  
    close  getspid  
    deallocate  getspid  
    end  执行时:
    exec killspid 'lower'
    再执行RESTORE