📄 dbconfig.pas
字号:
end;
//选择数据库备份盘符路径
procedure TForm_DBConfig.DriveComboBox1Change(Sender: TObject);
begin
DirectoryListBox1.Drive:=DriveComboBox1.Drive;
end;
//备份和还原数据库
procedure TForm_DBConfig.Btn_backupClick(Sender: TObject);
var
tmpstr:string; //备份数据库的 SQL 语句
database_name:string; //待备份数据库名称
restore_dbname:string;//备份后文件名称
begin
if not DataMod.ADOCon.Connected then
begin
try
DataMod.ADOCon.Connected:=true;
except
Msgerr(handle,'数据库连接失败,请先测试连接数据库!');
exit;
end;
end;
database_name:=trim(edt_dbname.Text);
if database_name='' then
begin
Msgok(handle,'待备份数据库名称不能为空,请先测试连接数据库!');
exit;
end;
restore_dbname:=trim(Edt_backname.Text);
//得到备份数数据库的路径
tmpstr:=DirectoryListBox1.GetItemPath(DirectoryListBox1.ItemIndex);
if tmpstr='' then
begin
Msgok(handle,'请选择数据库路径!');
exit;
end;
//////////////////////////////////////////////////////////////////////////
{--This example creates a logical backup device in which a full backup of the MyNwind database is placed.
-- Create the backup device for the full MyNwind backup.
'Enterprise_db_1',
'c:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Enterprise_db_1.dat'
--Create the log backup device.
USE master
EXEC sp_addumpdevice 'disk', 'MyNwindLog1', --------------sp_copysubscription
'c:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Enterprise_db_1_log.dat'
-- Back up the full MyNwind database.
BACKUP DATABASE Enterprise_db TO Enterprise_db_1
-- Update activity has occurred since the full database backup.
-- Back up the log of the MyNwind database.
BACKUP LOG Enterprise_db
TO Enterprise_db_1_log
}
//////////////////////////////////////////////////////////////////////////
case RadioGroup1.ItemIndex of
0:
with ADOQ_Backup do
begin
if restore_dbname='' then
begin
Msgerr(handle,'请输入备份数据库的文件名称!');
exit;
end;
if fileexists(tmpstr+'\'+restore_dbname+'.dat') then
begin
if Msgqst(handle,'该备份文件名已存在,是否覆盖?')=idno then
begin
abort; exit;
end;
end;
close; sql.Clear;
sql.Add('USE master EXEC sp_addumpdevice ''disk'', '''+restore_dbname+''',');
sql.Add(''''+tmpstr+'\'+restore_dbname+'.dat''');
sql.Add('backup database '+database_name+' to '+restore_dbname);
try
execsql;
except
Msgerr(handle,'数据库备份失败,请重试!');
exit;
end;
Msgok(handle,'数据库备份成功!');
end;
//////////////////////////////////////////////////////////////////////////
{
USE master
GO
-- First determine the number and names of the files in the backup.
-- MyNwind_2 is the name of the backup device.
RESTORE FILELISTONLY
FROM MyNwind_2
-- Restore the files for MyNwind2_Test.
RESTORE DATABASE MyNwind2_Test
FROM MyNwind_2
WITH RECOVERY,
MOVE 'MyNwind2_data' TO 'D:\MyData\MyNwind2_Test_data.mdf',
MOVE 'MyNwind2_log' TO 'D:\MyData\MyNwind2_Test_log.ldf'
GO
USE master
EXEC sp_addumpdevice 'disk', 'MyNwindLog1', --------------sp_copysubscription
'c:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Enterprise_db_1_log.dat'
sp_restoredbreplication [ @srv_orig = ] 'original_server_name'
, [ @db_orig = ] 'original_database_name'
[ , [ @keep_replication = ] keep_replication ]
BACKUP DATABASE Northwind
TO DISK = 'c:\Northwind.bak'
RESTORE FILELISTONLY
FROM DISK = 'c:\Northwind.bak'
RESTORE DATABASE TestDB
FROM DISK = 'c:\Northwind.bak'
WITH MOVE 'Northwind' TO 'c:\test\testdb.mdf',
MOVE 'Northwind_log' TO 'c:\test\testdb.ldf'
GO
}
/////////////////////////////////////////////////////////////////////////////
1:
begin
{ with ADOQ_Backup do
begin
close;sql.clear;
sql.add('RESTORE DATABASE tmpdb FROM :a WITH NORECOVERY');
parameters.ParamValues['a']:=DirectoryListBox1.Directory +'\Enterprise_db_20050330.dat';
try
execsql;
except
MsgErr(handle,'err 1');
exit;
end;
close;sql.clear;
sql.add(' MOVE tmpdb TO :c');
parameters.ParamValues['c']:='c:\NewNwind.mdf';
try
execsql;
except
MsgErr(handle,'err 2');
exit;
end;
end; }
// Msgok(handle,'数据库还原成功!');
end;
end;
end;
procedure TForm_DBConfig.RadioGroup1Click(Sender: TObject);
var tmpdatetime:Tdatetime;
begin
case RadioGroup1.ItemIndex of
0:
begin
GroupBox1.Caption:='数据库备份文件保存路径选择';
Lbl_backname.Caption:='待备份数据库名称:';
Btn_backup.Caption:='开始备份&B';
edt_dbname.ReadOnly:=true;
edt_dbname.ParentColor:=true;
Label4.visible:=true;
Edt_backname.visible:=true;
if edit_dbname.Text<>'' then edt_dbname.Text:=Edit_DBName.Text;
if edt_dbname.Text<>'' then
begin
GetHostDateTime(DataMod.ADOCon,tmpdatetime);
Edt_backname.Text:=edt_dbname.Text+'_'+formatdatetime('yyyymmdd',tmpdatetime);
end;
end;
1:
begin
GroupBox1.Caption:='还原数据库的文件路径选择';
Lbl_backname.Caption:='还原后数据库名称:';
Btn_backup.Caption:='开始还原&R';
edt_dbname.ReadOnly:=false;
edt_dbname.ParentColor:=false;
edt_dbname.Color:=clwindow;
Label4.visible:=false;
Edt_backname.visible:=false;
end;
end;
end;
end.
////////////////////////////////////////////////
//Delphi 备份数据库的方法
////////////////////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
begin
with IBRestoreService1 do
begin
ServerName := 'Poulet';
LoginPrompt := False;
Params.Add('user_name=sysdba');
Params.Add('password=masterkey');
Active := True;
try
Verbose := True;
Options := [Replace, UseAllSpace];
PageBuffers := 3000;
PageSize := 4096;
DatabaseName.Add('c:\interbase\tutorial\tutorial.gdb');
BackupFile.Add('c:\interbase\tutorial\backups\tutor5.gbk');
ServiceStart;
While not Eof do
Memo1.Lines.Add(GetNextLine);
finally
Active := False;
end;
end;
end;
//To restore a database from multiple files:
procedure TForm1.Button3Click(Sender: TObject);
begin
with IBRestoreService1 do
begin
ServerName := 'Poulet';
LoginPrompt := False;
Params.Add('user_name=sysdba');
Params.Add('password=masterkey');
Active := True;
try
Verbose := True;
Options := [Replace, UseAllSpace];
PageBuffers := 3000;
PageSize := 4096;
BackupFile.Add('c:\temp\e1.gbk');
BackupFile.Add('c:\temp\e2.gbk');
BackupFile.Add('c:\temp\e3.gbk');
DatabaseName.Add('c:\interbase\examples\database\employee.gdb');
ServiceStart;
While not Eof do
Memo1.Lines.Add(GetNextLine);
finally
Active := False;
end;
end;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -