📄 mybackuprestoredata.cpp
字号:
// MyBackupRestoreData.cpp : 实现文件
//
#include "stdafx.h"
#include "ScoreGather.h"
#include "MyBackupRestoreData.h"
#include ".\mybackuprestoredata.h"
// CMyBackupRestoreData 对话框
IMPLEMENT_DYNAMIC(CMyBackupRestoreData, CDialog)
CMyBackupRestoreData::CMyBackupRestoreData(CWnd* pParent /*=NULL*/)
: CDialog(CMyBackupRestoreData::IDD, pParent)
, type(FALSE)
{
}
CMyBackupRestoreData::~CMyBackupRestoreData()
{
}
void CMyBackupRestoreData::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PROGRESS1, m_progress1);
DDX_Control(pDX, IDC_BROWSE, m_browse);
DDX_Control(pDX, IDC_START, m_start);
DDX_Control(pDX, IDCANCEL, m_cancel);
}
BEGIN_MESSAGE_MAP(CMyBackupRestoreData, CDialog)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
ON_BN_CLICKED(IDC_START, OnBnClickedStart)
ON_BN_CLICKED(IDC_BROWSE, OnBnClickedBrowse)
ON_EN_SETFOCUS(IDC_PATH_NAME, OnEnSetfocusPathName)
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
// CMyBackupRestoreData 消息处理程序
BOOL CMyBackupRestoreData::OnInitDialog()
{
CDialog::OnInitDialog();
m_tooltip.Create(this,TTS_ALWAYSTIP);
m_tooltip.Activate(TRUE);
m_tooltip.AddTool(GetDlgItem(IDC_BROWSE),IDS_BROWSE);
m_tooltip.AddTool(GetDlgItem(IDC_PATH_NAME),IDS_PATH_NAME);
m_tooltip.AddTool(GetDlgItem(IDC_START),IDS_START);
m_tooltip.AddTool(GetDlgItem(IDC_PROGRESS1),IDS_PROGRESS1);
m_tooltip.AddTool(GetDlgItem(IDCANCEL),IDS_CANCEL);
if(type)
{
SetWindowText("数据恢复==>选择数据文件后单击[恢复]按钮开始恢复");
SetDlgItemText(IDC_BROWSE,"选 择 上 次 备 份 的 文 件");
SetDlgItemText(IDC_START,"恢 复");
}
return TRUE;
}
void CMyBackupRestoreData::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
//OnOK();
}
void CMyBackupRestoreData::OnBnClickedCancel()
{
CDialog::OnCancel();
}
void CMyBackupRestoreData::OnBnClickedStart()
{
CString PathName;
GetDlgItemText(IDC_PATH_NAME,PathName);
if(!PathName.IsEmpty())
{
m_progress1.SetPos(0);
m_progress1.SetRange(0,100);
m_progress1.SetStep(25);
CString strSQL;
CString ctrl;
GetDlgItemText(IDC_START,ctrl);
int position;
CString string_file,string_left,sPathName;
do
{
position=PathName.Find('\\',0);
string_left=PathName.Left(position)+"\\\\";
string_file+=string_left;
PathName=PathName.Right(PathName.GetLength()-position-1);
}
while(position>0);
sPathName=string_file.Left(string_file.GetLength()-2)+PathName;
//theApp.m_pConn->Close();//断开数据库连接
detach_database();//分离数据库
if(ctrl=="备 份")
{
m_progress1.StepIt();
strSQL="BACKUP DATABASE ScoreGAther TO disk=";
CString bakpath,file1,file2,file1a,file2a;
bakpath=sPathName.Left(sPathName.ReverseFind('\\')+1);
file1=sPathName.Left(sPathName.GetLength()-5)+"=database"+sPathName.Right(5);
file2=sPathName.Left(sPathName.GetLength()-5)+"=log"+sPathName.Right(5);
strSQL=strSQL+"'"+sPathName.Left(sPathName.GetLength()-5)+"=database"+sPathName.Right(5)+"'";
m_progress1.StepIt();
(((CScoreGatherApp*)AfxGetApp())->m_pConn)->Execute((_bstr_t)strSQL,NULL,adCmdText);
m_progress1.StepIt();
strSQL="BACKUP LOG ScoreGAther TO disk=";
strSQL=strSQL+"'"+sPathName.Left(sPathName.GetLength()-5)+"=log"+sPathName.Right(5)+"'";
(((CScoreGatherApp*)AfxGetApp())->m_pConn)->Execute((_bstr_t)strSQL,NULL,adCmdText);
CString filea,fileb;
filea=Lzari.Merge(bakpath,file1,file2);//文件合并
fileb=filea.Left(filea.ReverseFind('.'))+".zipl";
Lzari.Compress(filea,fileb);//数据压缩
m_progress1.StepIt();
theApp.makedatabase();//连接数据库
AfxMessageBox("所有数据备份完毕!");
OnBnClickedCancel();
}
else
{
CString NewPathName=sPathName.Left(sPathName.ReverseFind('.')+1)+"blk";
Lzari.UnCompress(sPathName,NewPathName);
CString file_data,file_log;
file_data=sPathName.Left(sPathName.ReverseFind('.'))+"_database.blk";
file_log=sPathName.Left(sPathName.ReverseFind('.'))+"_log.blk";
m_progress1.StepIt();
Lzari.Partition(NewPathName,file_data,file_log);
m_progress1.StepIt();
theApp.m_pConn->Close();//断开数据库连接
strSQL="RESTORE DATABASE ScoreGAther FROM disk=";
CString string_file,string_left,PathName,sPathName;
strSQL=strSQL+"'"+file_data+"'";
strSQL=strSQL+" WITH NORECOVERY";
(((CScoreGatherApp*)AfxGetApp())->m_pConn)->Execute((_bstr_t)strSQL,NULL,adCmdText);
m_progress1.StepIt();
strSQL="RESTORE LOG ScoreGAther FROM disk=";
strSQL=strSQL+"'"+file_log+"'";
strSQL=strSQL+" WITH NORECOVERY";
(((CScoreGatherApp*)AfxGetApp())->m_pConn)->Execute((_bstr_t)strSQL,NULL,adCmdText);
m_progress1.StepIt();
theApp.makedatabase();//连接数据库
AfxMessageBox("所有数据恢复完毕!");
OnBnClickedCancel();
}
}
}
CString CMyBackupRestoreData::detach_database()
{
/*
USE master;
GO
EXEC sp_detach_db @dbname = N'pubs';
GO
CString strSQL="USE master;\r\n";
strSQL+="GO\r\n";
strSQL+="EXEC sp_detach_db @dbname = N'ScoreGather';\r\n";
strSQL+="GO";
CString strSQL="EXEC sp_detach_db @dbname = N'ScoreGather';";
AfxMessageBox(strSQL);
theApp.m_pConn->Execute(_bstr_t(strSQL),NULL,adCmdStoredProc);*/
return CString();
}
CString CMyBackupRestoreData::attach_database()
{
/*
USE master;
Go
EXEC sp_attach_db @dbname = N'pubs',
@filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf',
@filename2 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_log.ldf';
GO
*/
return CString();
}
void CMyBackupRestoreData::OnBnClickedBrowse()
{
SetDlgItemText(IDC_PATH_NAME,"");
BOOL return_type=TRUE;
CString ctrl;
GetDlgItemText(IDC_START,ctrl);
if(ctrl=="备 份")
{
CString Directory="";
LPMALLOC lpMalloc;
CHAR szDisplayName[MAX_PATH];
CHAR szBuffer[MAX_PATH];
if ( ::SHGetMalloc( &lpMalloc ) != NOERROR )
{
AfxMessageBox( "执行路径错误,请退出后重试!" );
return;
}
BROWSEINFO BrowseInfo;
ZeroMemory(&BrowseInfo,sizeof(BROWSEINFO));
BrowseInfo.hwndOwner = GetSafeHwnd();
BrowseInfo.pidlRoot = NULL;
BrowseInfo.pszDisplayName = szDisplayName;
BrowseInfo.lpszTitle = "==== 选择存放备份文件的位置后单击[确定]按钮 ====";
BrowseInfo.ulFlags = BIF_USENEWUI;
BrowseInfo.lpfn = NULL;
BrowseInfo.lParam = 0;
LPITEMIDLIST lpItemIDList;
if ( ( lpItemIDList = ::SHBrowseForFolder( &BrowseInfo ) ) != NULL )
{
if ( ::SHGetPathFromIDList( lpItemIDList, szBuffer ) )
{
if ( szBuffer[0] == '\0' )
{
AfxMessageBox( "无效路径,重新选择!");
return_type=FALSE;
}
Directory = szBuffer;
}
else
{
AfxMessageBox( "无效路径,请重新选择!");
return_type=FALSE;
}
lpMalloc->Free( lpItemIDList );
lpMalloc->Release();
if(Directory.GetLength()<1)
return_type=FALSE;
else
{
if(Directory.GetLength()>3)
Directory+="\\";
SYSTEMTIME t;
GetSystemTime(&t);
t.wHour+=8;
if(t.wHour>=24)
if(t.wHour==24)
t.wHour=0;
else
t.wHour-=24;
CString m_sysdate,m_systime;
m_sysdate.Format("%4d年%02d月%02d日",t.wYear,t.wMonth,t.wDay);
m_systime.Format("%02d时%02d分%02d秒[教学成绩管理系统]数据备份.zipl",t.wHour,t.wMinute,t.wSecond);
SetDlgItemText(IDC_PATH_NAME,Directory+m_sysdate+m_systime);
}
}
}
else
{
CString filter="数据备份文件(*.zipl)|*.zipl|";
CFileDialog file(TRUE,NULL,NULL,OFN_HIDEREADONLY,filter);
if(file.DoModal()==IDOK)
SetDlgItemText(IDC_PATH_NAME,file.GetPathName());
}
if(!return_type)
OnBnClickedBrowse();
}
void CMyBackupRestoreData::OnEnSetfocusPathName()
{
GotoDlgCtrl(GetDlgItem(IDC_PROGRESS1));
}
BOOL CMyBackupRestoreData::PreTranslateMessage(MSG* pMsg)
{
m_tooltip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
BOOL CMyBackupRestoreData::OnEraseBkgnd(CDC* pDC)
{
CBrush brush(RGB(250,220,250));
CBrush* pOldBrush=pDC->SelectObject(&brush);
CRect rcClip;
pDC->GetClipBox(&rcClip);
pDC->PatBlt(rcClip.left,rcClip.top,rcClip.Width(),rcClip.Height(),PATCOPY);
pDC->SelectObject(pOldBrush);
return TRUE;
}
HBRUSH CMyBackupRestoreData::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if(nCtlColor!=CTLCOLOR_EDIT)
{
pDC->SetTextColor(RGB(40,20,255));
pDC->SetBkMode(TRANSPARENT);
HBRUSH B = CreateSolidBrush(RGB(250,220,250));
return (HBRUSH) B;
}
else
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
void CMyBackupRestoreData::OnOK()
{
}
void CMyBackupRestoreData::OnCancel()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -