再delphi如何备份数据。和还原数据,举实例

    技术2022-05-11  89

    sql server的也是别人的代码//备份procedure TForm1.BitBtn1Click(Sender: TObject);beginStatusBar1.SimpleText := '正在备份中...';//label1.Caption:='正在备份中...';pgbar.Max := 100;pgbar.Min := 0;pgbar.Position := 0;pgbar.Step := 20;savedialog1.Title:='备份到:';with SaveDialog1 dobeginfilename := database + FormatDateTime('yy-MM-dd-hh-mm-ss', Now);options := [ofhidereadonly, offilemustexist, ofpathmustexist];if execute thenbegindm.ADOConnection1.Close;//TmpStr:= ExtractFileName(filename);// strlen:= Length(TmpStr);// DevName:= Copy(TmpStr,1,strlen-4);begintryADOCommand1.CommandText := 'use Master';pgbar.StepIt;ADOCommand1.Execute;adocommand1.CommandText := 'execute sp_helpdevice';pgbar.StepIt;adocommand1.Execute;pgbar.stepit;adocommand1.CommandText := 'backup database ' + database + ' to disk=''' + filename + ''' with init';pgbar.StepIt;adocommand1.Execute;adocommand1.CommandText := 'Use ' + database;pgbar.StepIt;adocommand1.Execute;// Self.Hide;application.MessageBox(pchar('数据库已经成功备份到 ' + filename + '!'), Gsysname, mb_ok + mb_iconinformation);//ShowMessage();StatusBar1.SimpleText := '';pgbar.Position := 0;trydm.ADOConnection1.Connected := True;exceptbeginStatusBar1.SimpleText := '';application.MessageBox('无法重新接连数据库!', Gsysname, mb_ok + mb_iconerror);abort;end;end;self.Close;excepton Exception doif dm.ADOConnection1.InTransaction thendm.ADOConnection1.RollbackTrans;end;end;end;end;end;//还原procedure TForm1.BitBtn2Click(Sender: TObject);beginif application.MessageBox('请确保无其他程序正在使用本数据库,否则,还原无法进行!',Gsysname,mb_okcancel+mb_iconinformation)=idcancel thenbeginabort;end;StatusBar1.SimpleText := '正在还原中...';pgbar.Max := 100;pgbar.Min := 0;pgbar.Position := 0;pgbar.Step := 20;opendialog1.Title:='还原为:';with OpenDialog1 dobeginfilename := '';options := [ofhidereadonly, offilemustexist, ofpathmustexist];if execute thenbeginif dm.ADOConnection1.Connected thendm.ADOConnection1.Close;sleep(1000);pgbar.StepIt;tryADOCommand1.CommandText := 'use Master';ADOCommand1.Execute;sleep(600);pgbar.StepIt;adocommand1.CommandText := 'execute sp_helpdevice';adocommand1.Execute;sleep(600);pgbar.stepit;adocommand1.CommandText := 'Restore database ' + database + ' From disk=''' + filename + ''' with replace';sleep(600);pgbar.StepIt;tryadocommand1.Execute;exceptbeginraise exception.Create(syserrormessage(getlasterror));abort;end;end;adocommand1.CommandText := 'Use ' + database;sleep(600);pgbar.StepIt;adocommand1.Execute;StatusBar1.SimpleText := '';application.MessageBox(pchar('数据库已经成功还原到数据库 ' + database + '!'), Gsysname, mb_ok + mb_iconinformation);pgbar.Position := 0;trydm.ADOConnection1.Connected := True;exceptbeginStatusBar1.SimpleText := '';application.MessageBox('无法重新接连数据库!', Gsysname, mb_ok + mb_iconerror);abort;end;end;self.Close;excepton Exception doif dm.ADOConnection1.InTransaction thendm.ADOConnection1.RollbackTrans;end;end;end;end;

     

     

     

     

    还原首先要断开你连接数据库的全部connection,然后连接数据库到master,在执行你的还原语句看看这段代码!vardatabase:String;begin......database:='你的数据库';tryScreen.Cursor:=crHourGlass;if mDataModule.ADOConnection1.Connected thenmDataModule.ADOConnection1.Close;sleep(1000);ProgressBar2.StepIt;ADOCommand1.CommandText := 'use Master';ADOCommand1.Execute;sleep(600);ProgressBar2.StepIt;adocommand1.CommandText := 'execute sp_helpdevice';adocommand1.Execute;sleep(600);ProgressBar2.stepit;adocommand1.CommandText := 'Restore database ' + database + ' From disk=''' + myFilename + ''' with replace';sleep(600);ProgressBar2.StepIt;tryadocommand1.Execute;exceptbeginraise exception.Create(syserrormessage(getlasterror));abort;end;end;adocommand1.CommandText := 'Use ' + database;sleep(600);ProgressBar2.StepIt;adocommand1.Execute;StatusBar1.SimpleText := '';application.MessageBox(pchar('数据库已经成功还原到数据库 ' + database + '!'), '提示', mb_ok + mb_iconinformation);ProgressBar2.Position := 0;trymDataModule.ADOConnection1.Connected := True;exceptbeginapplication.MessageBox('无法重新接连数据库!', '提示', mb_ok + mb_iconerror);abort;end;end;exceptApplication.MessageBox('有别的程序(如企业管理器)正在使用本数据库,数据无法恢复','提示',MB_OK+MB_IconInformation);end;StatusBar1.SimpleText := '';frmMain.GetReportDate;Screen.Cursor:=crArrow;Self.Close;end;

     

     

     

     

    请问上面的大哥oracle数据库里面的表是怎么备份的呢?hackingExp system/manager@ORCL File=D:/Exp.DMP Log=D:/Exp.log //导出所有对象imp system/manager@ORCL file=D:/Exp.DMP log=D:/Exp.log //导入所有对象czx0514delphi里如何写牙bigben2008WinExec();里面的字符串可以自己组织的。yinweixian用控件adocommand,备份是backup,还原是restorefhuiboprocedure TForm1.BitBtn1Click(Sender: TObject);beginStatusBar1.SimpleText := '正在备份中...';//label1.Caption:='正在备份中...';pgbar.Max := 100;pgbar.Min := 0;pgbar.Position := 0;pgbar.Step := 20;savedialog1.Title:='备份到:';with SaveDialog1 dobeginfilename := database + FormatDateTime('yy-MM-dd-hh-mm-ss', Now);options := [ofhidereadonly, offilemustexist, ofpathmustexist];if execute thenbegindm.ADOConnection1.Close;//TmpStr:= ExtractFileName(filename);// strlen:= Length(TmpStr);// DevName:= Copy(TmpStr,1,strlen-4);begintryADOCommand1.CommandText := 'use Master';pgbar.StepIt;ADOCommand1.Execute;adocommand1.CommandText := 'execute sp_helpdevice';pgbar.StepIt;adocommand1.Execute;pgbar.stepit;adocommand1.CommandText := 'backup database ' + database + ' to disk=''' + filename + ''' with init';pgbar.StepIt;adocommand1.Execute;adocommand1.CommandText := 'Use ' + database;pgbar.StepIt;adocommand1.Execute;// Self.Hide;application.MessageBox(pchar('数据库已经成功备份到 ' + filename + '!'), Gsysname, mb_ok + mb_iconinformation);//ShowMessage();StatusBar1.SimpleText := '';pgbar.Position := 0;trydm.ADOConnection1.Connected := True;exceptbeginStatusBar1.SimpleText := '';application.MessageBox('无法重新接连数据库!', Gsysname, mb_ok + mb_iconerror);abort;end;end;self.Close;excepton Exception doif dm.ADOConnection1.InTransaction thendm.ADOConnection1.RollbackTrans;end;end;end;end;end;//还原procedure TForm1.BitBtn2Click(Sender: TObject);beginif application.MessageBox('请确保无其他程序正在使用本数据库,否则,还原无法进行!',Gsysname,mb_okcancel+mb_iconinformation)=idcancel thenbeginabort;end;StatusBar1.SimpleText := '正在还原中...';pgbar.Max := 100;pgbar.Min := 0;pgbar.Position := 0;pgbar.Step := 20;opendialog1.Title:='还原为:';with OpenDialog1 dobeginfilename := '';options := [ofhidereadonly, offilemustexist, ofpathmustexist];if execute thenbeginif dm.ADOConnection1.Connected thendm.ADOConnection1.Close;sleep(1000);pgbar.StepIt;tryADOCommand1.CommandText := 'use Master';ADOCommand1.Execute;sleep(600);pgbar.StepIt;adocommand1.CommandText := 'execute sp_helpdevice';adocommand1.Execute;sleep(600);pgbar.stepit;adocommand1.CommandText := 'Restore database ' + database + ' From disk=''' + filename + ''' with replace';sleep(600);pgbar.StepIt;tryadocommand1.Execute;exceptbeginraise exception.Create(syserrormessage(getlasterror));abort;end;end;adocommand1.CommandText := 'Use ' + database;sleep(600);pgbar.StepIt;adocommand1.Execute;StatusBar1.SimpleText := '';application.MessageBox(pchar('数据库已经成功还原到数据库 ' + database + '!'), Gsysname, mb_ok + mb_iconinformation);pgbar.Position := 0;trydm.ADOConnection1.Connected := True;exceptbeginStatusBar1.SimpleText := '';application.MessageBox('无法重新接连数据库!', Gsysname, mb_ok + mb_iconerror);abort;end;end;self.Close;excepton Exception doif dm.ADOConnection1.InTransaction thendm.ADOConnection1.RollbackTrans;end;end;end;end;fhuiboAccess数据库;varMyFileName: string;beginMyFileName := '';if SaveDialog1.Execute thenbeginMyFileName := SaveDialog1.FileName;if MyFileName <> '' thenbeginCopyFile(Pchar(ExtractFilePath(Application.ExeName) + 'shoufa.mdb'), Pchar(MyFileName), false);end;end;varMyFileName: string;beginMyFileName := '';if OpenDialog1.Execute thenbeginMyFileName := OpenDialog1.FileName;if MyFileName <> '' thenbeginCopyFile(Pchar(MyFileName),Pchar(ExtractFilePath(Application.ExeName) + 'shoufa.mdb'),false);end;end;end;godnoloveihavelovesql server这样写的背份是BACKUP DATABASE BASENAME TO DISK。。。。恢复是RESTORE DATABASE BASENAME FROM DISK。。。这是从磁盘是这样写的,你可以看TSQL帮助yinzhiworacle怎么写啊?zgq19801123upliulangnanhai代碼怎麼那麼長啊?我的代碼:constfilename = './baili.bat';com1 = ' net stop mssqlserver ';com2 = ' Net start mssqlserver ';vars2: string;s: Tstringlist;begintryif opend.Execute then //saved是savedialog控件s2 := opend.FileNameelseexit;if not fileexists(filename) thenbegins := Tstringlist.Create();s.Clear;s.Add(com1);s.Add(com2);s.SaveToFile(filename);end;application.ProcessMessages;winexec(filename, 9);sleep(20000);basedm.ADOConnection1.Connected := False;Basedm.ADOConnection1.Connected := True;adocommand1.CommandText := 'USE master RESTORE DATABASE Baili FROM DISK=''' + s2 + ''' WITH RECOVERY USE BAILI';self.pre2;adocommand1.Execute;messagedlg('資料還原成功!', mtinformation, [mbok], 0);exceptMessageDlg('還原資料失敗!' + #13 + #10 + '程序將仍用原資料庫運行!', mtError, [mbOK], 0);end;  


    最新回复(0)