首页  编辑  

SQL SERVER 2000 数据库备份与恢复单元

Tags: /超级猛料/Database.数据库相关/SQL Server/   Date Created:

unit BackRestoreSQLDB;

  {SERVER 2000 ,SERVER 2000 +Delphi7.0

 

 

 }

interface

uses

  adodb,db;

function repl_substr(sub_old, sub_new, s: string ): string ; //

function BackupSQLDataBase(connstr_sql,DatabaseName,Backup_FileName: string ):Boolean; //

//SQL

//Backup_FileName

function RestoreSQLDataBase(connstr_sql,DatabaseName,Restore_FileName: string ):Boolean; //

//Restore_FileName

implementation

function repl_substr(sub_old, sub_new, s: string ): string ;

var

  i: integer;

begin

 repeat

    i := pos(sub_old, s);

    if i > 0 then

   begin

      delete(s, i, Length(sub_old));

     insert(sub_new, s, i);

    end ;

  until i < 1;

 Result := s;

end ;

function BackupSQLDataBase(connstr_sql,DatabaseName,Backup_FileName: string ):Boolean;

var

//

//Backup_FileName

aADOQuery:TADOQuery;

begin

try

  aADOQuery:=TADOQuery.Create( nil );

 aADOQuery.Close;

 aADOQuery.ConnectionString:=connstr_sql;

 aADOQuery.SQL.Clear;

 aADOQuery.SQL.Add('backup database '+DatabaseName+' to disk = ' + '''' + Backup_FileName + ''' with format');

  Try

    aADOQuery.ExecSQL;

 Result:=true;

  Except

    Result:=false;

  exit;

  end ;

finally

  aADOQuery.Free;

end ;

end ;

function RestoreSQLDataBase(connstr_sql,DatabaseName,Restore_FileName: string ):Boolean;

var //

aADOQuery:TADOQuery;

begin

try

  aADOQuery:=TADOQuery.Create( nil );

 aADOQuery.Close; //

  aADOQuery.ConnectionString:=repl_substr(DatabaseName,'master',connstr_sql);

 aADOQuery.SQL.Clear;

 aADOQuery.SQL.Add('RESTORE DATABASE '+DatabaseName+' from disk = ' + '''' + Restore_FileName + '''');

Try

    aADOQuery.ExecSQL;

 Result:=true;

Except

    Result:=false;

  exit;

  end ;

finally

  aADOQuery.Free;

end ;

end ;

end .