诸位CSDN的大大好!
   小弟我有一个问题想请教!
  我有一个数据库,数据如下:
   id     name   money   .....
   0001   张三   1200请问:如何依照现有数据库的数据,创建新的数据库,并更新其中的数据?保存新的数据库以当月为名,如:200601.mdb  
下一次打开,如果是在本月,则不新建,如果已经到下个月了,则新建如 200602.mdb 的数据库,并更新所有数据?跪求相关代码,越详细越好!谢谢!

解决方案 »

  1.   

    先将文件copy成另外一个文件名,再连接新文件,删除,修改……
      

  2.   

    现判断时间,是本月则放弃,如果不是则COPY一个,改名字,再进行操作
      

  3.   

    关于新建MDB数据库的代码,可以参考
    http://blog.csdn.net/wudi_1982/archive/2006/05/31/764880.aspx
      

  4.   

    每次连接时个做判断
    判断数据库名
    再判断一下数据库是否存在,不在就Create DataBaae DataBase_Name
      

  5.   

    看看这个代码,是完整的数据库备份,跟你所说的备份方式是一样的。---------------------------------------------------------------------------unit BackupDBFrm;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, BaseFrm, Menus, cxLookAndFeelPainters, StdCtrls, cxButtons,
      cxControls, cxContainer, cxEdit, cxTextEdit, ExtCtrls,ConstUtils,
      PublicUtils;
    const
      
      BACKUP = 0;  //数据备份
      RESTORE = 1;  //数据恢复
      
    type
      TBackupDBForm = class(TBaseForm)
        openDL: TOpenDialog;
        cxButton1: TcxButton;
        ButDataPath: TcxButton;
        edtoldData: TcxTextEdit;
        btnClose: TcxButton;
        btnLogin: TcxButton;
        Bevel: TBevel;
        edtNewData: TcxTextEdit;
        procedure btnLoginClick(Sender: TObject);
        procedure ButDataPathClick(Sender: TObject);
        procedure cxButton1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        //备份与还原状态值
        BRConst : integer;    //取路径
        Dir:string;    //显示备份与还原状态
        Procedure MainShow;    //
        Procedure GetDir(path:string;BorR:integer);    //取得数据库加系统时间
        Function GetmdfName(Path:string):string;    //    procedure LoadConstBR;
      public
        { Public declarations }
      end;implementation{$R *.dfm}procedure TBackupDBForm.FormCreate(Sender: TObject);
    begin
      inherited;
      BRConst := StrToInt(GetParam('BackupRestore'));
      MainShow;
      openDL.Filter := 'Access数据库文件(*.mdb)|*.mdb';
      openDL.InitialDir := ExtractFileDir(Application.ExeName);
    end;procedure TBackupDBForm.btnLoginClick(Sender: TObject);
    begin
      inherited;
      LoadConstBR;
    end;procedure TBackupDBForm.ButDataPathClick(Sender: TObject);
    begin
      inherited;
      if edtoldData.Text <> '' then
      begin
    //    if BRConst = BACKUP then
    //    begin
    //      if Not DirectoryExists('数据库备份路径!', '', Dir) then Exit;
    //      GetDir(Dir, BACKUP);
    //    end
    //    else
    //    if BRConst = RESTORE then
    //    begin
    //      if Not DirectoryExists('数据库还原路径!', '', Dir) then Exit;
    //      edtNewData.Text := dir;
    //      GetDir(Dir, RESTORE);
    //    end;
      end
      else
      begin
      if BRConst = BACKUP then
      begin
        ShowMsg('请选择需要备份的数据库!');
        edtoldData.SetFocus;
      end
      else
      if BRConst = RESTORE then
      begin
        ShowMsg('请选择需要还原的数据库!\');
        edtoldData.SetFocus;
      end;
      end;
    end;procedure TBackupDBForm.cxButton1Click(Sender: TObject);
    begin
      inherited;
      if Not openDL.Execute then Exit;
      edtoldData.Text := openDL.FileName;
    end;procedure TBackupDBForm.GetDir(path: string; BorR: integer);
    begin
      shortdateformat := 'yymmdd';   //格式化日期  2004.5.10 -> 040510
      if BRConst = BACKUP then
      begin
      if (StrLen(PChar(Path)) <> 3) then
        edtNewData.Text := path + '\' +GetmdfName(edtOldData.Text) +
            DatetoStr(date) + '.mdb'
      else
        edtNewData.Text := path + GetmdfName(edtOldData.Text) + DatetoStr(date) +
           '.mdb';
      end
      else
      if BRConst = RESTORE then
      begin
      if (StrLen(PChar(Path)) <> 3) then
        edtNewData.Text := path + '\BgStudent.mdb'
      else
        edtNewData.Text := path + 'BgStudent.mdb';
      end
    end;function TBackupDBForm.GetmdfName(Path: string): string;
    var
      s1: string;
    begin
      s1 := Path;
      while Pos('\', s1) <> 0 do    // c:\aa\bb\cc.mdb
        s1 := copy(s1, pos('\', s1) + 1,length(s1));
        result := copy(s1, 0, Pos('.', s1) - 1);
    end;procedure TBackupDBForm.LoadConstBR;
    begin
      if edtNewData.Text = '' then
      begin
        if BRConst = BACKUP then
        begin
        ShowMsg('请选择需要备份数据库的路径!');
        edtNewData.SetFocus ;
        exit;
        end
      else
      if BRConst= RESTORE then
        begin
        ShowMsg('请选择需要还原数据库的路径!');
        edtNewData.SetFocus;
        exit;
        end;
      end;  try
      if FileExists(Trim(edtNewData.Text)) then
      begin
      if application.MessageBox('你是否要覆盖吗?', '系统提示',
          mb_yesno + mb_iconquestion) = ID_NO then
        exit
      else
      begin
        CopyFile(PChar(edtOldData.Text),PChar(edtNewData.Text),false);
        if BRConst = BACKUP then
          ShowMsg('数据库备份成功!'     )
        else
        if BRConst = RESTORE then
          ModalResult := mrOk;
       end ;
      end
      else
      begin
        CopyFile(PChar(edtOldData.Text),PChar(edtNewData.Text),false);
        if BRConst=BACKUP then
          ShowMsg('数据库备份成功!'        )
        else
        if BRConst=RESTORE then
          ModalResult := mrOk;
      end;
      except    if BRConst = BACKUP then
        begin
          ShowMsg('请选择需要备份的数据库!              ');
          edtoldData.SetFocus;
        end
        else
        if BRConst= RESTORE then
        begin
          ShowMsg('请选择需要还原的数据库!               ');
          edtNewData.SetFocus;
        end;  
      end;
      end;  procedure TBackupDBForm.MainShow;
      begin
      if BRConst=BACKUP then
      begin
        self.Caption:='备份数据库';
        ButDataPath.Caption:='备份路径';
        openDL.Title:='选择需要备份的数据库';
      end
      else
      if BRConst=RESTORE then
      begin
        self.Caption:='还原数据库';
        ButDataPath.Caption:='还原路径';
        openDL.Title:='选择需要还原的数据库';
      end;
    end;initialization  RegisterClass(TBackupDBForm);finalization  UnRegisterClass(TBackupDBForm);end.---------------------------------------------------------------------------