📄 db_main.pas
字号:
unit DB_Main;
{
代码单元名称:通用数据库备份工具主窗口
从属软件:大连资金清算中心打码机管理信息系统
开发单位:大连理工大学计算机技术研究所软件工程研究室
作者:王树润
时间:2001,1,30
}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls, Buttons, inifiles;
type
TDB_DfmMain = class(TForm)
PageControl1: TPageControl;
GeneralTabSheet: TTabSheet;
OptionTabSheet: TTabSheet;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Label1: TLabel;
DatabaseNameComboBox: TComboBox;
Label2: TLabel;
AfterBackupDatabaseNameEdit: TEdit;
BackupModeRadioGroup: TRadioGroup;
GroupBox1: TGroupBox;
DestinationListBox: TListBox;
DiskRadioButton: TRadioButton;
Button4: TButton;
Button5: TButton;
CoverRadioGroup: TRadioGroup;
GroupBox2: TGroupBox;
RemoveLogCheckBox: TCheckBox;
CheckBox4: TCheckBox;
CheckExpirationCheckBox: TCheckBox;
Label3: TLabel;
InicializeCheckBox: TCheckBox;
Label4: TLabel;
Label5: TLabel;
DescriptionEdit: TEdit;
SetExpirantionDateTimePicker: TDateTimePicker;
Label6: TLabel;
mediadescriptionEdit: TEdit;
mediaNameEdit1: TEdit;
SpeedButton1: TSpeedButton;
ScheduleCheckBox: TCheckBox;
ScheduleEdit: TEdit;
SetExpirationCheckBox: TCheckBox;
Label7: TLabel;
mediaNameEdit2: TEdit;
Animate1: TAnimate;
procedure Button2Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure DatabaseNameComboBoxChange(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure BackupModeRadioGroupClick(Sender: TObject);
procedure CoverRadioGroupClick(Sender: TObject);
procedure InicializeCheckBoxClick(Sender: TObject);
procedure ScheduleCheckBoxClick(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyServerName: string;
BackupTimeType: string;
BackupTimeSql: string;
end;
procedure SetWork;
implementation
uses DB_BackupDestination, DB_DataModule1, DB_DestinationContent, Main,
DB_BackupSchedule, PublicUnit;
{$R *.DFM}
procedure SetWork;
var
DB_DfmMain: TDB_DfmMain;
ServerName: string;
begin
if
MessageDlg('该项功能必须在数据库所在的机器上进行,您当前的机器是数据库所在的机器吗?',
mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
Exit;
ServerName := FrmMain.ServerName;
if ServerName = '' then
Exit;
Screen.Cursor := crHourGlass;
FrmMain.Refresh;
Application.CreateForm(TDB_DfmMain, DB_DfmMain);
DB_DfmMain.MyServerName := ServerName;
Screen.Cursor := crDefault;
DB_DfmMain.ShowModal;
DB_DfmMain.Free;
end;
//窗体==========================================================================
procedure TDB_DfmMain.FormShow(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
Refresh;
DatabaseNameComboBox.Items.Add(FrmMain.DbName);
DatabaseNameComboBox.ItemIndex := 0;
BackupModeRadioGroup.ItemIndex := 0;
CoverRadioGroup.ItemIndex := 0;
DiskRadioButton.Checked := True;
DiskRadioButton.Enabled := False;
RemoveLogCheckBox.Enabled := False;
SetExpirationCheckBox.Enabled := False;
SetExpirantionDateTimePicker.Enabled := False;
Label7.Enabled := False;
InicializeCheckBox.Enabled := False;
mediaNameEdit1.Enabled := False;
Label4.Enabled := False;
Label6.Enabled := False;
mediadescriptionEdit.Enabled := False;
CheckExpirationCheckBox.Checked := True;
AfterBackupDatabaseNameEdit.Text := DatabaseNameComboBox.Text + ' backup';
CheckBox4.Checked := True;
ScheduleCheckBox.Checked := False;
SpeedButton1.Enabled := False;
Screen.Cursor := crDefault;
end;
procedure TDB_DfmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DB_DfmDataModule1.ADOQuery1.Close;
DB_DfmDataModule1.ADOConnection1.Close;
end;
//关闭==========================================================================
procedure TDB_DfmMain.Button2Click(Sender: TObject);
begin
Close;
end;
//生成备份后库名================================================================
procedure TDB_DfmMain.DatabaseNameComboBoxChange(Sender: TObject);
begin
AfterBackupDatabaseNameEdit.Text := DatabaseNameComboBox.Text + ' backup';
end;
//添加备份设备==================================================================
procedure TDB_DfmMain.Button4Click(Sender: TObject);
var
DB_DfmBackupDestination: TDB_DfmBackupDestination;
begin
if DestinationListBox.Items.Count <> 0 then
Exit;
DB_DfmDataModule1.ADOConnection1.Close;
DB_DfmBackupDestination := TDB_DfmBackupDestination.Create(Self,
DiskRadioButton, DestinationListBox, MyServerName);
DB_DfmBackupDestination.ShowModal;
DB_DfmBackupDestination.Free;
end;
//删除备份设备==================================================================
procedure TDB_DfmMain.Button5Click(Sender: TObject);
begin
if DestinationListBox.ItemIndex <> -1 then
DestinationListBox.Items.Delete(DestinationListBox.ItemIndex);
end;
//查看备份设备内容==============================================================
procedure TDB_DfmMain.Button6Click(Sender: TObject);
var
DB_DfmDestinationContent: TDB_DfmDestinationContent;
begin
if DestinationListBox.ItemIndex = -1 then
Exit;
DB_DfmDestinationContent := TDB_DfmDestinationContent.Create(Self,
DestinationListBox.Items[DestinationListBox.ItemIndex], MyServerName);
DB_DfmDestinationContent.ShowModal;
DB_DfmDestinationContent.Free;
end;
//各选项变灰功能================================================================
procedure TDB_DfmMain.BackupModeRadioGroupClick(Sender: TObject);
begin
if BackupModeRadioGroup.ItemIndex = 2 then
begin
RemoveLogCheckBox.Enabled := True;
RemoveLogCheckBox.Checked := True;
end
else
begin
RemoveLogCheckBox.Enabled := False;
RemoveLogCheckBox.Checked := False;
end;
end;
procedure TDB_DfmMain.CoverRadioGroupClick(Sender: TObject);
begin
if CoverRadioGroup.ItemIndex = 0 then
begin
SetExpirationCheckBox.Enabled := False;
SetExpirationCheckBox.Checked := False;
SetExpirantionDateTimePicker.Enabled := False;
Label7.Enabled := False;
InicializeCheckBox.Enabled := False;
InicializeCheckBox.Checked := False;
mediaNameEdit1.Enabled := False;
Label4.Enabled := False;
Label6.Enabled := False;
mediadescriptionEdit.Enabled := False;
end
else
begin
SetExpirationCheckBox.Enabled := True;
SetExpirantionDateTimePicker.Enabled := True;
Label7.Enabled := True;
InicializeCheckBox.Enabled := True;
mediaNameEdit1.Enabled := True;
Label4.Enabled := True;
Label6.Enabled := True;
mediadescriptionEdit.Enabled := True;
end;
end;
procedure TDB_DfmMain.InicializeCheckBoxClick(Sender: TObject);
begin
if InicializeCheckBox.Checked = True then
begin
CheckExpirationCheckBox.Enabled := False;
CheckExpirationCheckBox.Checked := False;
mediaNameEdit2.Enabled := False;
Label3.Enabled := False;
end
else
begin
CheckExpirationCheckBox.Enabled := True;
CheckExpirationCheckBox.Checked := True;
mediaNameEdit2.Enabled := True;
Label3.Enabled := True;
end;
end;
//备份时间选项==================================================================
procedure TDB_DfmMain.ScheduleCheckBoxClick(Sender: TObject);
begin
if ScheduleCheckBox.Checked = True then
begin
SpeedButton1.Enabled := True;
end
else
begin
SpeedButton1.Enabled := False;
ScheduleEdit.Clear;
end;
end;
//设置备份时间==================================================================
procedure TDB_DfmMain.SpeedButton1Click(Sender: TObject);
var
DB_DfmBackupSchedule: TDB_DfmBackupSchedule;
begin
DB_DfmBackupSchedule := TDB_DfmBackupSchedule.Create(Self);
if DB_DfmBackupSchedule.ShowModal = mrOK then
begin
BackupTimeType := DB_DfmBackupSchedule.BackupTimeType;
ScheduleEdit.Text := DB_DfmBackupSchedule.BackupTime;
end;
DB_DfmBackupSchedule.Free;
end;
//备份开始======================================================================
procedure TDB_DfmMain.Button1Click(Sender: TObject);
var
WangshurunFile: TIniFile;
i: integer;
SqlStrings: TStrings;
begin
DB_DfmDataModule1.ADOQuery1.Close;
DB_DfmDataModule1.ADOConnection1.Close;
SqlStrings := TStringList.Create;
if DestinationListBox.Items.Count = 0 then
begin
ShowMessage('请输入备份设备名称!');
Exit;
end;
for i := 0 to DestinationListBox.Items.Count - 1 do
begin
if BackupModeRadioGroup.ItemIndex = 2 then
SqlStrings.Add('backup log ' + DatabaseNameComboBox.Text)
else
SqlStrings.Add('backup database ' + DatabaseNameComboBox.Text);
SqlStrings.Add('to ' + DestinationListBox.Items[i]);
SqlStrings.Add('with ');
SqlStrings.Add('name=''' + AfterBackupDatabaseNameEdit.Text + '''');
SqlStrings.Add(',description=''' + DescriptionEdit.Text + '''');
if BackupModeRadioGroup.ItemIndex = 1 then
SqlStrings.Add(',differential');
if SetExpirationCheckBox.Checked = True then
SqlStrings.Add(',expiredate=''' +
TimeToStr(SetExpirantionDateTimePicker.DateTime) + '''');
if InicializeCheckBox.Checked = True then
begin
SqlStrings.Add(',Format');
SqlStrings.Add(',mediadescription=''' + mediadescriptionEdit.Text + '''');
SqlStrings.Add(',mediaName=''' + mediaNameEdit1.Text + '''');
end
else
SqlStrings.Add(',NoFormat');
if CoverRadioGroup.ItemIndex = 1 then
SqlStrings.Add(',init');
if CheckExpirationCheckBox.Checked = True then
begin
SqlStrings.Add(',noskip');
SqlStrings.Add(',mediaName=''' + mediaNameEdit2.Text + '''');
end;
if BackupModeRadioGroup.ItemIndex = 2 then
begin
if RemoveLogCheckBox.Checked = False then
SqlStrings.Add(',no_truncate');
end;
end;
DB_DfmDataModule1.ADOConnection1.ConnectionString :=
'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=' + FrmMain.DbUser
+
';Password=' + FrmMain.DbPass + ';Initial Catalog=master;Data Source=' +
PublicUnit.ServerName;
Animate1.Visible := True;
Animate1.Active := True;
with DB_DfmDataModule1.ADOQuery1 do
begin
Close;
Sql.Clear;
Sql.Assign(SqlStrings);
Screen.Cursor := crHourGlass;
try
ExecSQL;
except
Screen.Cursor := crDefault;
Animate1.Active := False;
Animate1.Visible := False;
ShowMessage('备份不成功,备份设备不能使用或备份操作不当!');
Exit;
end;
end;
Screen.Cursor := crDefault;
for i := 0 to SqlStrings.Count - 1 do
BackupTimeSql := BackupTimeSql + ' ' + SqlStrings[i];
SqlStrings.Free;
Animate1.Active := False;
Animate1.Visible := False;
ShowMessage('备份完成!');
//定时备份参数================================================================
if ScheduleCheckBox.Checked = True then
begin
WangshurunFile := TIniFile.Create('C:\WangshurunFile.ini');
WangshurunFile.WriteString('BackupTimeParameter', 'ServerName',
MyServerName);
WangshurunFile.WriteString('BackupTimeParameter', 'BackupDatabaseName',
DatabaseNameComboBox.Text);
WangshurunFile.WriteString('BackupTimeParameter', 'BackupTimeType',
BackupTimeType);
WangshurunFile.WriteString('BackupTimeParameter', 'BackupSql',
BackupTimeSql);
WangshurunFile.WriteString('BackupTimeParameter', 'BackupTime',
ScheduleEdit.Text);
end;
Close;
end;
//设定定时备份任务==============================================================
procedure TDB_DfmMain.Button3Click(Sender: TObject);
var
WangshurunFile: TIniFile;
i: integer;
SqlStrings: TStrings;
begin
if ScheduleCheckBox.Checked = False then
Exit;
SqlStrings := TStringList.Create;
if DestinationListBox.Items.Count = 0 then
begin
ShowMessage('请输入备份设备名称!');
Exit;
end;
for i := 0 to DestinationListBox.Items.Count - 1 do
begin
if BackupModeRadioGroup.ItemIndex = 2 then
SqlStrings.Add('backup log ' + DatabaseNameComboBox.Text)
else
SqlStrings.Add('backup database ' + DatabaseNameComboBox.Text);
SqlStrings.Add('to ' + DestinationListBox.Items[i]);
SqlStrings.Add('with ');
SqlStrings.Add('name=''' + AfterBackupDatabaseNameEdit.Text + '''');
SqlStrings.Add(',description=''' + DescriptionEdit.Text + '''');
if BackupModeRadioGroup.ItemIndex = 1 then
SqlStrings.Add(',differential');
if SetExpirationCheckBox.Checked = True then
SqlStrings.Add(',expiredate=''' +
TimeToStr(SetExpirantionDateTimePicker.DateTime) + '''');
if InicializeCheckBox.Checked = True then
begin
SqlStrings.Add(',Format');
SqlStrings.Add(',mediadescription=''' + mediadescriptionEdit.Text + '''');
SqlStrings.Add(',mediaName=''' + mediaNameEdit1.Text + '''');
end
else
SqlStrings.Add(',NoFormat');
if CoverRadioGroup.ItemIndex = 1 then
SqlStrings.Add(',init');
if CheckExpirationCheckBox.Checked = True then
begin
SqlStrings.Add(',noskip');
SqlStrings.Add(',mediaName=''' + mediaNameEdit2.Text + '''');
end;
if BackupModeRadioGroup.ItemIndex = 2 then
begin
if RemoveLogCheckBox.Checked = False then
SqlStrings.Add(',no_truncate');
end;
end;
for i := 0 to SqlStrings.Count - 1 do
BackupTimeSql := BackupTimeSql + ' ' + SqlStrings[i];
SqlStrings.Free;
//定时备份参数================================================================
WangshurunFile := TIniFile.Create('C:\WangshurunFile.ini');
WangshurunFile.WriteString('BackupTimeParameter', 'ServerName', MyServerName);
WangshurunFile.WriteString('BackupTimeParameter', 'BackupDatabaseName',
DatabaseNameComboBox.Text);
WangshurunFile.WriteString('BackupTimeParameter', 'DbUser',
PublicUnit.DBUser);
WangshurunFile.WriteString('BackupTimeParameter', 'DbPass',
PublicUnit.DBPass);
WangshurunFile.WriteString('BackupTimeParameter', 'BackupTimeType',
BackupTimeType);
WangshurunFile.WriteString('BackupTimeParameter', 'BackupSql', BackupTimeSql);
WangshurunFile.WriteString('BackupTimeParameter', 'BackupTime',
ScheduleEdit.Text);
Close;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -