📄 trunform.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <DateUtils.hpp>
#include <Registry.hpp>
#include <assert.h>
#pragma hdrstop
#include "TRunForm.h"
#include "TMainForm.h"
#include "stdafx.h"
#include "ClientConnect.h"
#define BACKUP_CHECK_SPAN 1000
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TRunForm *RunForm;
//---------------------------------------------------------------------------
AnsiString g_Path;
AnsiString g_ConnectionString;
char *g_IPFormat = "%d.%d.%d.%d";
char *g_CreateDirError = "备份文件目录创建失败,请检查相关设置。";
int g_nIP1 , g_nIP2 , g_nIP3 , g_nIP4;
BOOL CheckCreateDir(const AnsiString &strPath)
{
if(!DirectoryExists(strPath))
{
if(!CreateDir(strPath))
{
return FALSE;
}
}
return TRUE;
}
__fastcall TRunForm::TRunForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TRunForm::meuEditTaskClick(TObject *Sender)
{
TMainForm *Dlg = new TMainForm(NULL);
Dlg->ShowModal();
delete Dlg;
}
//---------------------------------------------------------------------------
void __fastcall TRunForm::meuExitClick(TObject *Sender)
{
if(MessageBox(NULL , "退出SQL备份系统后,份计划将不能被执行。" , "提示" ,MB_YESNO | MB_DEFBUTTON2 | MB_ICONQUESTION ) == IDYES)
{
Close();
}
}
//---------------------------------------------------------------------------
void __fastcall TRunForm::FormCreate(TObject *Sender)
{
g_nIP1 = 0;
g_nIP2 = 0;
g_nIP3 = 0;
g_nIP4 = 0;
g_Path = ExtractFilePath(Application->ExeName);
g_ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + g_Path + "BackupTask.mdb;Persist Security Info=False";
LoadIP();
m_nSecond = 0;
m_dtLastTime = Now();
HANDLE hThread = CreateThread(NULL , 0 , LoadTodayTask , (LPVOID)&m_dtLastTime , 0 , NULL);
CloseHandle(hThread);
SetAutoRun();
}
//---------------------------------------------------------------------------
void __fastcall TRunForm::WndProc(Messages::TMessage & Message)
{
if(Message.Msg == WM_SYSCOMMAND)
{
if(Message.WParam == SC_MINIMIZE)
{
Hide();
return;
}
}
TForm::WndProc(Message);
}
void __fastcall TRunForm::FormActivate(TObject *Sender)
{
PostMessage(Handle , WM_SYSCOMMAND , SC_MINIMIZE , 0);
}
//---------------------------------------------------------------------------
void TRunForm::LoadIP()
{
MyConnection pConn(new TADOConnection(NULL));
MyDataSet pData(new TADODataSet(NULL));
pConn->ConnectionString = g_ConnectionString;
pConn->Open();
pData->Connection = pConn.get();
pData->CommandText = "SELECT * FROM ServerAddress";
pData->Open();
if(pData->Eof == FALSE)
{
g_nIP1 = pData->FieldByName("IP1")->AsInteger;
g_nIP2 = pData->FieldByName("IP2")->AsInteger;
g_nIP3 = pData->FieldByName("IP3")->AsInteger;
g_nIP4 = pData->FieldByName("IP4")->AsInteger;
}
else
{
g_nIP1 = 0;
g_nIP2 = 0;
g_nIP3 = 0;
g_nIP4 = 0;
}
pData->Close();
}
void __fastcall TRunForm::TimerTimer(TObject *Sender)
{
if(m_nSecond % 60 == 0)
{
m_nSecond++;
m_bConnServer = FALSE;
if(g_nIP1 != 0)
{
AnsiString strIP;
strIP.sprintf(g_IPFormat , g_nIP1 , g_nIP2 , g_nIP3 , g_nIP4 );
ClientConnect ServerConnect;
if(ServerConnect.Connect(strIP.c_str()))
{
m_bConnServer = TRUE;
}
}
SetTrayIcon();
}
else
if(m_nSecond > 300)
{
m_nSecond = 0;
TDateTime dtCurrTime(Now());
unsigned short nLastYear, nLastMonth, nLastDay;
unsigned short nCurrYear, nCurrMonth, nCurrDay;
m_dtLastTime.DecodeDate(&nLastYear, &nLastMonth, &nLastDay);
dtCurrTime.DecodeDate(&nCurrYear, &nCurrMonth, &nCurrDay);
if(nLastDay != nCurrDay)
{
m_dtLastTime = dtCurrTime;
HANDLE hThread = CreateThread(NULL , 0 , LoadTodayTask , (LPVOID)&m_dtLastTime , 0 , NULL);
CloseHandle(hThread);
}
}
else
m_nSecond++;
}
//---------------------------------------------------------------------------
void TRunForm::SetTrayIcon()
{
if(m_bConnServer)
{
Tray->IconIndex = 0;
Tray->Hint = "与SQL服务器连接就绪";
}
else
{
Tray->IconIndex = 1;
Tray->Hint = "不能连接到SQL服务器";
}
}
DWORD WINAPI TRunForm::LoadTodayTask(LPVOID lpParam)
{
InitCom _init_com;
MyConnection pConn(new TADOConnection(NULL));
MyDataSet pData(new TADODataSet(NULL));
TDateTime dtCurrTime;
dtCurrTime = *((TDateTime *)lpParam);
try{
pConn->ConnectionString = g_ConnectionString;
pConn->Open();
pData->Connection = pConn.get();
pData->CommandText = "SELECT * FROM Task WHERE ActiveDate <= #" + dtCurrTime.DateString() + "# ORDER BY ActiveDate , ActiveTime";
pData->Open();
}
catch(...)
{
return MessageBox(NULL , "系统错误:不能打开数据库" , "错误" , MB_OK | MB_ICONERROR);
return (DWORD)0;
}
TList *pList = new TList;
BackupTask *pTask;
int i;
TDateTime dtSysTime;
for(pData->First() ; !pData->Eof ; pData->Next())
{
pTask = new BackupTask;
pTask->SetID(pData->FieldByName("ID")->AsInteger );
pTask->SetDatabase(pData->FieldByName("DatabaseName")->AsString );
pTask->SetCycleType(pData->FieldByName("CycleType")->AsInteger );
pTask->SetCycleCount(pData->FieldByName("CycleCount")->AsInteger );
pTask->SetDayCount(pData->FieldByName("DayCount")->AsInteger );
pTask->SetActiveTime(pData->FieldByName("ActiveTime")->AsDateTime);
pTask->SetActiveDate(pData->FieldByName("ActiveDate")->AsDateTime);
pList->Add(pTask);
}
pData->Close();
while(pList->Count)
{
dtSysTime = Now();
pTask =(BackupTask *)pList->First();
if( pTask->GetActiveDate() < DateOf(dtSysTime))
{
//备份
if(StartBackup(pTask))
{
BackupSucceedHandle(pConn.get() , pTask);
}
pList->Remove(pTask);
delete pTask;
}
else
if(pTask->GetActiveTime() < TimeOf(dtSysTime))
{
//备份
if(StartBackup(pTask))
{
BackupSucceedHandle(pConn.get() , pTask);
}
pList->Remove(pTask);
delete pTask;
}
Sleep(BACKUP_CHECK_SPAN);
}
delete pList;
return 0;
}
BOOL TRunForm::StartBackup(BackupTask * pTask)
{
AnsiString strIP;
AnsiString strBackupPath;
strIP.sprintf(g_IPFormat , g_nIP1 , g_nIP2 , g_nIP3 , g_nIP4 );
ClientConnect ServerConnect;
strBackupPath = g_Path + "Backup\\";
if(CheckCreateDir(strBackupPath) == FALSE)
{
MessageBox(NULL , g_CreateDirError , "错误" , MB_OK |MB_ICONERROR);
return FALSE;
}
strBackupPath += pTask->GetDatabase() + '\\';
if(CheckCreateDir(strBackupPath) == FALSE)
{
MessageBox(NULL , g_CreateDirError , "错误" , MB_OK |MB_ICONERROR);
return FALSE;
}
strBackupPath += pTask->GetDatabase() + pTask->GetActiveDate().DateString() + ".bak";
if(!ServerConnect.Connect(strIP.c_str()))
{
MessageBox(NULL , "不能连接备份服务器,请检查相关设置。" , "错误" , MB_OK |MB_ICONERROR);
return FALSE;
}
if(!ServerConnect.Backup(pTask->GetDatabase().c_str() , strBackupPath.c_str()))
{
MessageBox(NULL , ServerConnect.GetError().c_str() , "错误" , MB_OK |MB_ICONERROR);
return FALSE;
}
return TRUE;
}
void TRunForm::BackupSucceedHandle(TADOConnection * pConn, BackupTask * pTask)
{
AnsiString strSql;
strSql = "UPDATE Task SET ActiveDate=#" + pTask->GetNextDate().DateString() + "# WHERE ID=" +
IntToStr(pTask->GetID());
int nAffected;
pConn->Execute(strSql , nAffected);
if(nAffected != 1)
{
MessageBox(NULL , "修改下一次备份时间出错,请与软件开发商联系。" , "错误" , MB_OK |MB_ICONERROR);
}
}
void TRunForm::SetAutoRun()
{
TRegistry *pReg = new TRegistry;
pReg->RootKey = HKEY_LOCAL_MACHINE;
if(pReg->OpenKey("\\Software\\Microsoft\\Windows\\CurrentVersion\\Run" , FALSE))
{
pReg->WriteString("SQLBackupClient" , Application->ExeName);
pReg->CloseKey();
}
delete pReg;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -