如何实现备份数据库和恢复数据库????

解决方案 »

  1.   

    sqlserver的?他自带有restore和backup存储过程,你可以看一下帮助!
      

  2.   

    备份:
    adoquery1.SQL.Add('backup database 数据库名 to Disk='''+filename+'''');恢复:
    adoquery1.sql.add('restore database 数据库名 from disk='''+FileName+'''');
      

  3.   

    如果是用access实现呢?
    是用copyfile实现吗??
      

  4.   

    如果是用access,可以用copyfile直接把文件考出备份
      

  5.   

    用Access,最好将数据记录按照时间段导如一个文件中,这样文件很小,方便用软盘备份(好多单位用Access的系统不是很大,需要用到软盘)
      

  6.   

    数据记录按照时间段导如一个文件中,该怎么导?Access没导过
      

  7.   

    关注!!小弟遇到同样的问题![email protected]共同研究了/!
      

  8.   

    数据库备份:
    var
        ls_sql: string;
        logfile: textfile;
        adoquery1: TADOQuery;
    begin
        //edtpath1:备份所用目录的输入框
        if not DirectoryExists(edtpath1.Text) then
        begin
            if application.MessageBox('所输入文件夹不存在,是否创建?', '系统提示', mb_yesno) = idyes then
            begin
                MkDir(edtpath1.Text);
                if IOResult <> 0 then
                begin
                    MessageDlg('创建文件夹失败!', mtWarning, [mbOk], 0);
                    exit;
                end;
            end
            else
                Exit;
        end;    adoquery1 := TADOQuery.Create(self);
        //Pb_ShjdConn是全局的数据库连接
        adoquery1.Connection := Pb_ShjdConn;
        try
            ls_sql := 'BACKUP DATABASE newbase TO disk=''' + edtpath1.Text + '\newbase_' + formatdatetime('yymmddhhmm', now) + '.bak' + '''' + ' with format';
            adoquery1.SQL.Clear;
            adoquery1.sql.Add(ls_sql);
            adoquery1.Prepared;
            adoquery1.ExecSQL;
            application.MessageBox('数据库备份成功完成!', '数据备份', mb_ok);
        except on E: Exception do
            begin
                application.MessageBox('数据库备份失败!', '错误', mb_ok);
                AssignFile(logfile, ExtractFilePath(Application.ExeName) + '\backuperr.log');
                if FileExists(ExtractFilePath(Application.ExeName) + '\backuperr.log') then Append(logfile)
                else Rewrite(logfile);
                writeln(logfile, formatdatetime('yyyymmddhhmmss', now) + ':数据备份时:' + E.MESSAGE);
                closefile(logfile);
            end;
        end;
    end;
      

  9.   

    数据库恢复:
    var
        ls_sql: string;
        logfile: textfile;
        adoquery1: TADOQuery;
        ado_ShjdConn: TADOConnection;
    begin
        //edtpath2:备份文件所在路径
        if (copy(trim(edtpath2.Text), 2, 1) <> ':') and (copy(trim(edtpath2.Text), 1, 2) <> '\\') then
        begin
            application.MessageBox('路径输入不正确!', '系统提示', mb_ok);
            edtpath2.SetFocus;
            exit;
        end;
        if (copy(trim(edtpath2.Text), 3, 1) <> '\') and (copy(trim(edtpath2.Text), 1, 2) <> '\\') then
        begin
            application.MessageBox('路径输入不正确!', '系统提示', mb_ok);
            edtpath2.SetFocus;
            exit;
        end;
        try
            ado_ShjdConn := TAdoConnection.Create(application);
    //PbSTR_User:SQLSERVER数据库用户名,PbSTR_Pass:数据库密码        ado_ShjdConn.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=' + PbSTR_User + ';password=' + PbSTR_Pass + ';Initial Catalog=master';
            ado_ShjdConn.LoginPrompt := False;
            ado_ShjdConn.ConnectionTimeout := 5;
            ado_ShjdConn.Open;
        except
            application.MessageBox('连接master数据库失败!', '系统提示', mb_ok);
            exit;
        end;
        //关闭建立的数据库全局连接
        Pb_ShjdConn.Close;
        adoquery1 := TADOQuery.Create(self);
        adoquery1.Connection := ado_ShjdConn;
        try
            ls_sql := 'RESTORE DATABASE newbase FROM disk=''' + trim(edtpath2.Text) + '''';
            adoquery1.SQL.Clear;
            adoquery1.sql.Add(ls_sql);
            adoquery1.Prepared;
            adoquery1.ExecSQL;
            application.MessageBox('数据库恢复成功完成!', '系统提示', mb_ok);
        except on E: Exception do
            begin
                application.MessageBox('数据库恢复失败!', '错误', mb_ok);
                AssignFile(logfile, ExtractFilePath(Application.ExeName) + '\backuperr.log');
                if FileExists(ExtractFilePath(Application.ExeName) + '\backuperr.log') then Append(logfile)
                else Rewrite(logfile);
                writeln(logfile, formatdatetime('yyyymmddhhmmss', now) + ':数据恢复时:' + E.MESSAGE);
                closefile(logfile);
            end;
        end;
    end;
      

  10.   

    在恢复时候edtpath2中的是备份文件所在路径和文件名称