📄 viewsys.cpp
字号:
// ViewSys.cpp : implementation file
//
#include "stdafx.h"
#include "STUGRADE.h"
#include "ViewSys.h"
#include "MainFrm.h"
#include "DLGaddlogin.h"
#include "DlgLogin.h"
#include "TlHelp32.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CViewSys
extern CMainFrame *pFrame;
IMPLEMENT_DYNCREATE(CViewSys, CFormView)
CViewSys::CViewSys()
: CFormView(CViewSys::IDD)
{
//{{AFX_DATA_INIT(CViewSys)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CViewSys::~CViewSys()
{
}
void CViewSys::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CViewSys)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CViewSys, CFormView)
//{{AFX_MSG_MAP(CViewSys)
ON_BN_CLICKED(IDC_backup, Onbackup)
ON_BN_CLICKED(IDC_restore, Onrestore)
ON_BN_CLICKED(IDF_SYS_MANAGER, OnSysManager)
ON_BN_CLICKED(IDC_AddLogin, OnAddLogin)
ON_BN_CLICKED(IDC_ChangePass, OnChangePass)
ON_COMMAND(IDF_SYS_EXITUSER, OnSysExituser)
ON_COMMAND(ID_UserLogin, OnUserLogin)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CViewSys diagnostics
#ifdef _DEBUG
void CViewSys::AssertValid() const
{
CFormView::AssertValid();
}
void CViewSys::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CViewSys message handlers
void CViewSys::Onbackup()
{
int fc = pFrame->ConSQL.Find("SQL Server",0);
if (fc!=-1) //当前连接为SQL Server
{
CString szSQL;
CFileDialog dlgFileOpen(FALSE);
dlgFileOpen.m_ofn.lpstrTitle = "备份另存为";
dlgFileOpen.m_ofn.lpstrFilter ="所有文件 (*.*)\0*.*;\0DAT文件 (*.dat)\0*.dat\0";
if(dlgFileOpen.DoModal()==IDOK)
szSQL.Format("BACKUP DATABASE %s to DISK = '%s'",DatabaseName,(CString)dlgFileOpen.m_ofn.lpstrFile);
else
{
// MessageBox("备份文件名出错!",NULL,MB_ICONSTOP);
return;
}
try {
pFrame->ExecSQL(szSQL);
MessageBox("备份成功!",NULL,MB_ICONINFORMATION);
}
catch (_com_error e)
{
MessageBox("备份数据库失败!",NULL,MB_ICONSTOP);
}
}
else //当前为access连接
{
CString Dpath;
CFileDialog dlgFileOpen(FALSE);
dlgFileOpen.m_ofn.lpstrTitle = "MDB另存为";
dlgFileOpen.m_ofn.lpstrFilter ="Access数据文件 (*.mdb)\0*.mdb;\0";
if(dlgFileOpen.DoModal()==IDOK)
{
Dpath = (CString)dlgFileOpen.m_ofn.lpstrFile;
CString D_Right = Dpath.Right(3);
D_Right.MakeLower();
if (D_Right!="mdb")
Dpath.Insert(Dpath.GetLength(),".mdb");
}
else
{
// MessageBox("备份文件名出错!",NULL,MB_ICONSTOP);
return;
}
//AfxMessageBox(Dpath);
CString ConSQL,ConAccPath;
ConSQL = pFrame->ConSQL;
int fc = ConSQL.Find("Source=",0);
ConAccPath = ConSQL.Mid(fc+7,ConSQL.GetLength()-fc-7);
//ConAccPath = "H:\\dos.chm";
if (pFrame->m_pConnection->State)
pFrame->m_pConnection->Close();
CFile SF,DF;
CFileException ex;
if (!SF.Open(ConAccPath,
CFile::modeRead | CFile::shareDenyWrite, &ex))
{
SF.Close();
MessageBox("打开数据文件失败!");
return;
}
else
{
if (!DF.Open(Dpath, CFile::modeWrite |
CFile::shareExclusive | CFile::modeCreate, &ex))
{
MessageBox("打开备份文件失败!");
DF.Close();
return;
}
BYTE buffer[4096];
DWORD dwRead;
// Read in 4096-byte blocks,
// remember how many bytes were actually read,
// and try to write that many out. This loop ends
// when there are no more bytes to read.
do
{
dwRead = SF.Read(buffer, 4096);
DF.Write(buffer, dwRead);
}
while (dwRead > 0);
CString Msg; Msg.Format("Access数据库已备份到%s",Dpath);
MessageBox(Msg,"备份成功!",MB_ICONINFORMATION);
// Close both files
SF.Close();
DF.Close();
}//end if(!SF.open())
//重新打开连接
try{
pFrame->m_pConnection->ConnectionTimeout=5;
pFrame->m_pConnection->Open((LPCSTR)(pFrame->ConSQL),"","",adModeUnknown);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("数据库连接超时!\r\n请检查数据库状态!\r\n数据连接错误:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
}
void CViewSys::Onrestore()
{
int fc = pFrame->ConSQL.Find("SQL Server",0);
if (fc!=-1) //当前连接为SQL Server
{
CString ConSQL;
ConSQL = pFrame->ConSQL;
int sp = ConSQL.Find("DATABASE=",0);
sp+=9;
int len = pFrame->ConSQL.Find(";",sp)-sp;
// CString T_str = pFrame->ConSQL.Mid(sp,len);
ConSQL.Delete(sp,len);
ConSQL.Insert(sp,"master");
// ConSQL.Replace(DatabaseName,"master"); replace危险!
ConSQL.Insert(ConSQL.GetLength(),"@");
ConSQL.Insert(ConSQL.GetLength(),DatabaseName);
ConSQL.Insert(ConSQL.GetLength(),"#");
char str[255],strfile[255];
GetCurrentDirectory(255,str);
strcpy(strfile,str);
strcat(str,"\\restore.exe");
strcat(strfile,"\\restore.ini");
//写文件
CFile file;
CFileException ex;
if (!file.Open(strfile, CFile::modeWrite |
CFile::shareExclusive | CFile::modeCreate, &ex))
{
MessageBox("创建restore.ini失败!","恢复出错",MB_ICONSTOP);
file.Close();
return;
}
file.Write(ConSQL,ConSQL.GetLength());
file.Close();
HINSTANCE hst=ShellExecute(NULL,"open",str,0,0,SW_SHOW);
exit(0);
/*
if (!hst) // case 0:
MessageBox("恢复程序restore.exe内存溢出!","恢复出错",MB_ICONSTOP);
else if (hst==ERROR_FILE_NOT_FOUND)
MessageBox("找不到恢复程序restore.exe!","恢复出错",MB_ICONSTOP);
else if (hst==ERROR_PATH_NOT_FOUND)
MessageBox("找不到恢复程序restore.exe路径!","恢复出错",MB_ICONSTOP);
else if (hst==SE_ERR_OOM)
MessageBox("内存不足!","恢复出错",MB_ICONSTOP);
*/
//ExitWindows(0,0); //注销windows用户
// MessageBox("内存不足!","恢复出错",MB_ICONSTOP);
//ExitThread(0);
// ExitProcess(0); //打成release包时老是出错!!!
// exit(ID_APP_EXIT);
// exit(0);
/*
CString ConSQL;
ConSQL = pFrame->ConSQL;
ConSQL.Replace(DatabaseName,"master");
_ConnectionPtr m_pConectionMaster; //要打开其它的master连接
HRESULT hr;
try{
hr = m_pConectionMaster.CreateInstance(__uuidof( Connection )) ;
ASSERT(!FAILED(hr));
if(SUCCEEDED(hr)) {
m_pConectionMaster->ConnectionTimeout=5;
m_pConectionMaster->Open((LPCSTR)ConSQL,"","",adModeUnknown);
// m_pConnection->Open("driver={SQL Server};Server=hfx;DATABASE=StuGrade;UID=sa;PWD=604;","","",adModeUnknown);
// m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Stu.mdb","","",adModeUnknown);
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("Master数据库连接超时!\r\n请检查数据库状态!\r\n数据连接错误:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
CString szSQL;
CFileDialog dlgFileOpen(TRUE);
if(dlgFileOpen.DoModal()==IDOK) // WITH REPLACE
szSQL.Format("RESTORE DATABASE %s FROM DISK = '%s'",DatabaseName,(CString)dlgFileOpen.m_ofn.lpstrFile);
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -