CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  数据库相关

实时备份SQL数据库

楼主shirleyttt(兔兔)2005-08-17 16:15:31 在 Delphi / 数据库相关 提问

当数据库里有改变或有操作时,就备份到U盘上。 问题点数:100、回复次数:10Top

1 楼dengyueguang(小邓)回复于 2005-08-17 16:39:14 得分 5

没做过,以前做的都是手工备份。upTop

2 楼czx0514(不想再做那份工作)回复于 2005-08-17 17:08:17 得分 5

可以自己写一个事件,每当有数据变动的时候就把数据增量备份到目标设备上就可以了Top

3 楼n29882942(骑牛上北京)回复于 2005-08-17 17:10:14 得分 5

只做过定时备份,实时的没做过。实时的也没什么实际价值。  
  数据库一有攺变或操作时就自动备份,那计算机没时间干其他的了!Top

4 楼jackie168(http://soft.eastrise.net)回复于 2005-08-17 17:37:47 得分 0

unit   UnitBackup;  
   
  interface  
   
  uses  
      Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,  
      Db,ADODB,   ExtCtrls,   ShellApi,   Menus,  
      ComCtrls,   Registry,   ScktComp   ,   StdCtrls   ,   Buttons,     Variants,IniFiles   ,StrUtils,  
      ImgList   ;  
   
    const  
      WM_BARICON=WM_USER+200;   //自定义消息  
      ID_MAIN=100;//定义图标的ID  
  type  
      TFrmBackup   =   class(TForm)  
          GroupBox1:   TGroupBox;  
          Label1:   TLabel;  
          Label2:   TLabel;  
          Label3:   TLabel;  
          Label4:   TLabel;  
          CmbServerName:   TComboBox;  
          CmbDatabaseName:   TComboBox;  
          EdtUserName:   TEdit;  
          EdtPassword:   TEdit;  
          GroupBox2:   TGroupBox;  
          GroupBox3:   TGroupBox;  
          btnSave:   TBitBtn;  
          btnCancel:   TBitBtn;  
          cboAutoRun:   TCheckBox;  
          EdtPath:   TEdit;  
          btnPath:   TButton;  
          btnSetTime:   TBitBtn;  
          connAdo:   TADOConnection;  
          connQuery:   TADOQuery;  
          btnBackup:   TBitBtn;  
          Timer1:   TTimer;  
          cboShowMessage:   TCheckBox;  
          PopupMenu:   TPopupMenu;  
          N1:   TMenuItem;  
          N2:   TMenuItem;  
          ImageList1:   TImageList;  
          cboMin:   TCheckBox;  
          cboClose:   TCheckBox;  
          cboStart:   TCheckBox;  
          BitBtn1:   TBitBtn;  
          procedure   btnSetTimeClick(Sender:   TObject);  
          procedure   btnSaveClick(Sender:   TObject);  
          procedure   btnBackupClick(Sender:   TObject);  
          procedure   btnPathClick(Sender:   TObject);  
          procedure   Timer1Timer(Sender:   TObject);  
          procedure   FormCreate(Sender:   TObject);  
          procedure   btnCancelClick(Sender:   TObject);  
          procedure   N2Click(Sender:   TObject);  
          procedure   N1Click(Sender:   TObject);  
          procedure   FormCloseQuery(Sender:   TObject;   var   CanClose:   Boolean);  
          procedure   BitBtn1Click(Sender:   TObject);  
      private  
          {   Private   declarations   }  
          FilePath:String;  
          BackupIniFile:TIniFile;  
          ServerName,DatabaseName,UserName,Password:String;  
   
          Run,StartMin,Show,Min,CloseShow:integer;  
          Conn:Boolean;     //判断联接是否成功  
          connString:String;   //联接字符串  
          BackFileName:String;   //备份文件的名称  
          procedure   AddIcon(hwnd:HWND);  
          procedure   RemoveIcon(hwnd:   HWND);   //从状态区移去图标  
   
          procedure   WMSysCommand(var   Message:   TMessage);   message   WM_SYSCOMMAND;  
          procedure   WMBarIcon(var   Message:TMessage);message   WM_BARICON;  
          procedure   WMMini(var   Message:TMessage);message   WM_SETFOCUS;  
          procedure   BackupBase(FileName:String);   //定义备份过程  
          procedure   ReadIni;   //读取INI文件中的内容  
      public  
          {   Public   declarations   }  
          BackupType,EveryTime,EveryDay,EveryMonth,EveryWeek:integer;  
      end;  
   
  var  
      FrmBackup:   TFrmBackup;  
  implementation  
   
  uses   UnitFrmSetupTime,   UnitFilePath,   UnitDirPath,   UnitDlg;  
   
  {$R   *.dfm}  
  procedure   TFrmBackup.AddIcon(hwnd:HWND);  
  var  
      lpData:PNotifyIconData;  
  begin  
      lpData   :=   new(PNotifyIconDataA);  
      lpData.cbSize   :=   88;  
      lpData.Wnd   :=   FrmBackup.Handle;  
      lpData.hIcon   :=   FrmBackup.Icon.Handle;  
      lpData.uCallbackMessage   :=   WM_BARICON;  
      lpData.uID   :=0;  
      lpData.szTip   :=   '数据库备份工具';  
      lpData.uFlags   :=   NIF_ICON   or   NIF_MESSAGE   or   NIF_TIP;  
      Shell_NotifyIcon(NIM_ADD,lpData);  
      dispose(lpData);  
      FrmBackup.Visible   :=   False;  
  end;  
   
  procedure   TFrmBackup.RemoveIcon(hwnd:   HWND);//从状态区移去图标  
  var  
      lpData:PNotifyIconData;  
  begin  
      //如果用户点击任务栏图标则将图标删除并回复窗口。  
      lpData   :=   new(PNotifyIconDataA);  
      lpData.cbSize   :=   88;//SizeOf(PNotifyIconDataA);  
      lpData.Wnd   :=   FrmBackup.Handle;  
      lpData.hIcon   :=   FrmBackup.Icon.Handle;  
      lpData.uCallbackMessage   :=   WM_BARICON;  
      lpData.uID   :=0;  
      lpData.szTip   :=   'Samples';  
      lpData.uFlags   :=   NIF_ICON   or   NIF_MESSAGE   or   NIF_TIP;  
      Shell_NotifyIcon(NIM_DELETE,lpData);  
      dispose(lpData);  
  end;  
   
  procedure   TFrmBackup.WMSysCommand(var   Message:TMessage);  
  begin  
      if   Message.WParam   =   SC_ICON   then  
      begin  
            //如果用户最小化窗口则将窗口  
            //隐藏并在任务栏上添加图标  
            AddIcon(handle);  
      end  
      else   if   (Message.WParam=   SC_Close)   and   (Min=1)then   begin  
              Application.Minimize;  
              AddIcon(handle);  
      end  
      else   if   (Message.WParam=WM_CREATE)   and   (Min=1)then   begin  
              Application.Minimize;  
              AddIcon(handle);  
      end  
      else  
            //如果是其它的SystemCommand  
            //消息则调用系统缺省处理函数处理之。  
            DefWindowProc(FrmBackup.Handle,Message.Msg,Message.WParam,Message.LParam);  
  end;  
   
  procedure   TFrmBackup.WMBarIcon(var   Message:TMessage);  
  var  
        lpData:PNotifyIconData;  
        Pt:TPoint;  
  begin  
      if   (Message.LParam   =   WM_LBUTTONDOWN)   then  
        begin  
            RemoveIcon(handle);  
            FrmBackup.Visible   :=   True;  
        end  
        else   if   (Message.LParam=WM_RBUTTONDOWN)   then  
        begin  
            SetForeGroundWindow(lpData.Wnd);  
            GetCursorPos(Pt);  
            Popupmenu.Popup(Pt.X,pt.y);  
        end;  
  end;  
   
  procedure   TFrmBackup.WMMini(var   Message:TMessage);  
  begin  
      if   (Message.Msg=WM_CREATE)   then   begin  
              Application.Minimize;  
              AddIcon(handle);  
      end  
  end;  
   
  //定义备份过程  
  procedure   TFrmBackup.BackupBase(FileName:String);  
  begin  
      try  
          screen.Cursor:=crSqlWait;  
          with   connQuery   do  
          begin  
              close;  
              Sql.Text:='Backup   Database   '+Trim(CmbDatabaseName.Text)+'   to   Disk='''+FileName+'''   ';  
              ExecSQL;  
          end;  
          screen.Cursor:=crDefault;  
      except  
          MessageBox(handle,'备份失败!','提示',MB_IconWarning+mb_Ok);  
          screen.Cursor:=crDefault;  
          Exit;  
      end;  
  end;  
   
  //读取INI文件中的内容  
  procedure   TFrmBackup.ReadIni;  
  begin  
      FilePath:=ExtractFilePath(Application.ExeName)+'System.ini';  
      BackupIniFile:=TIniFile.Create(FilePath);  
      ServerName:=BackupIniFile.ReadString('Database','ServerName','');  
      DatabaseName:=BackupIniFile.ReadString('Database','DatabaseName','');  
      UserName:=BackupIniFile.ReadString('Database','UserName','');  
      Password:=BackupIniFile.ReadString('Database','Password','');  
      Run:=BackupIniFile.ReadInteger('AutoRun','Run',0);  
      StartMin:=BackupIniFile.ReadInteger('AutoRun','StartMin',0);    
      Show:=BackupIniFile.ReadInteger('AutoRun','Show',0);  
      Min:=BackupIniFile.ReadInteger('AutoRun','Min',0);  
      CloseShow:=BackupIniFile.ReadInteger('AutoRun','CloseShow',0);  
   
      BackupType:=BackupIniFile.ReadInteger('Backup','BackupType',0);  
      EveryTime:=BackupIniFile.ReadInteger('Backup','EveryTime',0);  
      EveryDay:=BackupIniFile.ReadInteger('Backup','EveryDay',0);  
      EveryWeek:=BackupIniFile.ReadInteger('Backup','EveryWeek',0);  
      EveryMonth:=BackupIniFile.ReadInteger('Backup','EveryMonth',0);  
   
      CmbServerName.Text:=ServerName;  
      CmbDatabaseName.Text:=DatabaseName;  
      EdtUserName.Text:=UserName;  
      EdtPassword.Text:=Password;  
      EdtPath.Text:=BackupIniFile.ReadString('FilePath','Path','');  
       
      if   Run=1   then     //初始化<随开机启动>  
          cboAutoRun.Checked:=True  
      else  
          cboAutoRun.Checked:=False;  
      if   StartMin=1   then   //初始化<启动后最小化>  
          cboStart.Checked:=True  
      else  
          cboStart.Checked:=False;  
      if   Show=1   then   //初始化<备份时显示提示信息>  
          cboShowMessage.Checked:=True  
      else  
          cboShowMessage.Checked:=False;  
      if   Min=1   then     //初始化<关闭时最小化>  
          cboMin.Checked:=True  
      else  
          cboMin.Checked:=False;  
      if   CloseShow=1   then     //初始化<不显示关闭信息>  
          cboClose.Checked:=True  
      else  
          cboClose.Checked:=False;  
  end;  
   
  procedure   TFrmBackup.btnSetTimeClick(Sender:   TObject);  
  begin  
      Application.CreateForm(TFrmSetupTime,FrmSetupTime);    
      FrmSetupTime.ShowModal;  
      FrmSetupTime.Free;  
  end;Top

5 楼jackie168(http://soft.eastrise.net)回复于 2005-08-17 17:38:25 得分 65

procedure   TFrmBackup.btnSaveClick(Sender:   TObject);  
  var  
      reg:TRegistry;  
      S_RegTree:String;  
  begin  
      if   not   conn   then   //判断数据库联接是否成功,只有成功之后才能保存  
      begin  
          MessageBox(handle,'数据库联接没有成功,所以不能保存!','错误',mb_IconWarning+mb_Ok);  
          CmbServerName.SetFocus;  
          Exit;  
      end;  
      //将数据库联接参数保存到Ini文件中  
      BackupIniFile.WriteString('Database','ServerName',Trim(CmbServername.Text));  
      BackupIniFile.WriteString('Database','DatabaseName',Trim(CmbDatabaseName.Text));  
      BackupIniFile.WriteString('Database','UserName',Trim(EdtUserName.Text));  
      BackupIniFile.WriteString('Database','Password',Trim(EdtPassword.Text));  
      BackupIniFile.WriteString('FilePath','Path',Trim(EdtPath.Text));  
      //判断是否开机时起动系统  
      reg:=tregistry.Create;  
      Reg.RootKey:=HKEY_LOCAL_MACHINE;  
      S_RegTree:='\Software\Microsoft\Windows\CurrentVersion\Run';  
      if   Reg.OpenKey(S_RegTree,False)=false   then  
          Reg.CreateKey(S_RegTree);  
      Reg.OpenKey(S_RegTree,True);  
      FilePath:=ExtractFilePath(Application.ExeName)+'Backup.exe';  
   
      if   cboAutoRun.Checked   then  
      begin  
          Reg.WriteString('BackupDatabase',FilePath);  
          BackupIniFile.WriteInteger('AutoRun','Run',1);  
      end  
      else   begin  
          Reg.DeleteValue('BackupDatabase');  
          BackupIniFile.WriteInteger('AutoRun','Run',0);  
      end;  
      //启动后最小化  
      if   cboStart.Checked   then  
          BackupIniFile.WriteInteger('AutoRun','StartMin',1)  
      else  
          BackupIniFile.WriteInteger('AutoRun','StartMin',0);  
      //<备份时显示提示信息>  
      if   cboShowMessage.Checked   then  
          BackupIniFile.WriteInteger('AutoRun','Show',1)  
      else  
          BackupIniFile.WriteInteger('AutoRun','Show',0);  
      //<关闭时最小化>  
      if   cboMin.Checked   then  
          BackupIniFile.WriteInteger('AutoRun','Min',1)  
      else  
          BackupIniFile.WriteInteger('AutoRun','Min',0);  
      //<显示关闭信息>  
      if   cboClose.Checked   then  
          BackupIniFile.WriteInteger('AutoRun','CloseShow',1)  
      else  
          BackupIniFile.WriteInteger('AutoRun','CloseShow',0);  
      Reg.Free;  
      ReadIni();  
  end;  
   
  procedure   TFrmBackup.btnBackupClick(Sender:   TObject);  
  var  
      FilePath:String;  
  begin  
      try  
          if   Show=1   then   begin  
              application.CreateForm(tfrmDlg,frmDlg);  
              frmDlg.Show;  
          end;  
   
          //文件名等于<年月日时分秒>  
          BackFileName:=Trim(CmbDatabaseName.Text)+FormatDateTime('yyyymmddhhmmss',Now);   //取得备份文件名  
          //取得地址框中的地址  
          if   RightStr(EdtPath.text,1)='\'   then  
                FilePath:=EdtPath.Text  
          else  
                FilePath:=EdtPath.Text+'\';  
   
          FilePath:=FilePath+BackFileName;  
          screen.Cursor:=crSqlWait;  
          BackupBase(FilePath);   //调用备份过程备份文件  
          screen.Cursor:=crDefault;  
          if   Show=1   then  
          begin  
              frmDlg.Close;  
              frmDlg.Free;  
          end;  
      except  
          MessageBox(handle,'备份失败!','提示',MB_IconWarning+mb_Ok);  
          screen.Cursor:=crDefault;  
          Exit;  
      end;  
  end;  
   
   
  procedure   TFrmBackup.btnPathClick(Sender:   TObject);  
  begin  
      Application.CreateForm(TFrmDirPath,FrmDirPath);    
      if   FrmDirPath.ShowModal=mrOk   then  
          EdtPath.Text:=FrmDirPath.DirectoryListBox1.Directory;  
  end;  
   
  procedure   TFrmBackup.Timer1Timer(Sender:   TObject);  
  var  
      Year,Month,Day,Hour,Min,Sec,MSec:Word;  
  begin  
      DecodeDate(Now,Year,Month,Day);  
      DecodeTime(Now,   Hour,   Min,   Sec,   MSec);  
   
      case   BackupType   of  
      0:begin//不备份  
              //  
          end;  
      1:begin//每小时  
              if   (Min=EveryTime)   and   (Sec=0)   then  
                  btnBackupClick(btnBackup);  
          end;  
      2:begin//每天  
              if   (Hour=EveryDay)   and   (Min=EveryTime)   and   (Sec=0)   then  
                  btnBackupClick(btnBackup);  
          end;  
      3:begin//每周  
              if   ((DayOfWeek(Date)-1)=EveryWeek)   and   (Hour=EveryDay)   and   (Min=EveryTime)   and   (Sec=0)   then  
                  btnBackupClick(btnBackup);  
          end;  
      4:begin//每月  
              if   (Day=EveryMonth)   and   (Hour=EveryDay)   and   (Min=EveryTime)   and   (Sec=0)   then  
                  btnBackupClick(btnBackup);  
          end;  
      end;  
   
  end;  
   
  procedure   TFrmBackup.FormCreate(Sender:   TObject);  
  begin  
      ReadIni();  
      connString:='Driver={SQL   server};server='+Trim(CmbServerName.Text)+';database='+Trim(CmbDatabaseName.Text)+  
                              ';uid='+Trim(EdtUserName.Text)+';pwd='+Trim(EdtPassword.Text);  
      try  
          connAdo.ConnectionString:=connString;  
          connAdo.LoginPrompt:=False;  
          connAdo.Connected:=True;  
          conn:=True;  
      except  
          MessageBox(handle,'联接数据库错误','错误',mb_IconWarning+mb_Ok);  
          Exit;  
      end;  
   
      //取得服务器中所有数据库名  
      with   connQuery   do  
      begin  
          Close;  
          Sql.Text:='select   *   from   master.dbo.sysdatabases';  
          open;  
          while   not   Eof   do  
          begin  
              cmbDatabaseName.Items.Add(Fields[0].Value);  
              next;  
          end;  
      end;  
      //判断启动时是否最小化窗体  
    {   if   StartMin=1   then  
      begin  
          //Application.Minimize;  
          AddIcon(handle);  
      end;     }  
  end;  
   
  procedure   TFrmBackup.btnCancelClick(Sender:   TObject);  
  begin  
      //取消所做的修改  
      ReadIni();  
  end;  
   
  procedure   TFrmBackup.N2Click(Sender:   TObject);  
  begin  
      close;  
  end;  
   
  procedure   TFrmBackup.N1Click(Sender:   TObject);  
  begin  
      FrmBackup.Visible   :=   True;  
  end;  
   
  procedure   TFrmBackup.FormCloseQuery(Sender:   TObject;  
      var   CanClose:   Boolean);  
  begin  
      if   CloseShow=1   then  
      begin  
          if   MessageBox(Handle,'确实要关闭系统吗?','提示',MB_YESNO+MB_ICONINFORMATION)=IDNO   then  
          begin  
              canClose:=False;  
          end  
          else  
          begin  
              RemoveIcon(handle);  
              Application.Terminate   ;  
          end;  
      end  
      else   begin  
          RemoveIcon(handle);  
          Application.Terminate   ;  
      end;  
  end;  
   
  procedure   TFrmBackup.BitBtn1Click(Sender:   TObject);  
  begin  
      if   Trim(CmbServerName.Text)=''   then  
      begin  
          MessageBox(handle,'数据库服务器名称不能为空!','错误',mb_IconWarning+mb_Ok);  
          CmbServerName.SetFocus;  
          Exit;  
      end;  
      if   Trim(CmbDatabaseName.Text)=''   then  
      begin  
          MessageBox(handle,'数据库名称不能为空!','错误',mb_IconWarning+mb_Ok);  
          CmbDatabaseName.SetFocus;  
          Exit;  
      end;  
      if   Trim(EdtUserName.Text)=''   then  
      begin  
          MessageBox(handle,'用户名不能为空!','错误',mb_IconWarning+mb_Ok);  
          EdtUserName.SetFocus;  
          Exit;  
      end;  
      connString:='Driver={SQL   server};server='+Trim(CmbServerName.Text)+';database='+Trim(CmbDatabaseName.Text)+  
                              ';uid='+Trim(EdtUserName.Text)+';pwd='+Trim(EdtPassword.Text);  
      try  
          screen.Cursor:=crSqlwait;  
          with   connAdo   do  
          begin  
              if   Connected   then  
                    Connected:=False;  
              ConnectionString:=connString;  
              LoginPrompt:=False;  
              Connected:=True;  
          end;  
          MessageBox(handle,'联接成功!','提示',MB_ICONINFORMATION+mb_Ok);  
          conn:=True;  
          screen.Cursor:=crdefault;  
      except  
          MessageBox(handle,'联接失败!','提示',mb_IconWarning+mb_Ok);  
          screen.Cursor:=crdefault;  
          conn:=False;  
          Exit;  
      end;  
  end;  
   
  end.Top

6 楼hanlin2004(道德守望者)回复于 2005-08-17 18:12:14 得分 5

so   长!Top

7 楼winxkm(蹩脚的程序员)回复于 2005-08-17 19:41:48 得分 5

建议你多看看SQLSERVER的资料,可以通过日志备份Top

8 楼hthunter(茫茫然时光飞逝)回复于 2005-08-17 20:18:27 得分 0

sql   server本身即有自动增量备份Top

9 楼hthunter(茫茫然时光飞逝)回复于 2005-08-17 20:19:25 得分 5

有增量备份这个功能,看看帮助吧,或者搜搜sql   server版的相关文章Top

10 楼scony1117(1117925)回复于 2005-08-17 21:59:14 得分 5

不是说SQLSERVER有日志文件吗?那还需要什么实时备份啊!定时的足够了!  
  如果是三层结构最多加个错误信息处理机制,或者加个缓冲区Top

相关问题

  • 原创SQL数据库实时备份!!!
  • sql数据库备份问题
  • 如何远程备份SQL数据库?
  • sql数据库备份恢复,急!急!急!
  • sql数据库备份恢复问题,急!急!急!
  • 关于MS SQL数据库备份的问题。
  • 求一段sql数据库备份得代码
  • asp如何实现自动备份sql数据库
  • 如何通过vb备份sql数据库?
  • [强!!!]如何利用ASP备份与恢复SQL数据库?

关键词

  • backupinifile
  • 备份
  • tfrmbackup
  • cmbdatabasename
  • lpdata
  • autorun
  • cmbservername
  • edtpath
  • readinteger
  • btnbackupclick

得分解答快速导航

  • 帖主:shirleyttt
  • dengyueguang
  • czx0514
  • n29882942
  • jackie168
  • hanlin2004
  • winxkm
  • hthunter
  • scony1117

相关链接

  • Delphi类图书
  • Delphi类源码下载
  • Delphi控件下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo