📄 viewsys.cpp
字号:
MessageBox("打开备份文件出错!",NULL,MB_ICONSTOP);
return;
}
try {
// pFrame->m_pConnection->Close();
//szSQL = "CREATE TABLE [dbo].[T_分科学分] ([学号] [nvarchar] (16) COLLATE Chinese_PRC_CI_AS NULL,[课程名称] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [课程学分] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL )";
m_pConectionMaster->Execute((LPCSTR)szSQL,NULL,adExecuteNoRecords);
MessageBox("还原成功!",NULL,MB_ICONINFORMATION);
}
catch (_com_error e)
{
MessageBox("还原数据库失败!",NULL,MB_ICONSTOP);
}
*/
}
else //当前为Access连接 还原
{
CString Spath;
CFileDialog dlgFileOpen(TRUE);
dlgFileOpen.m_ofn.lpstrTitle = "打开MDB数据库";
dlgFileOpen.m_ofn.lpstrFilter ="Access数据文件 (*.mdb)\0*.mdb;\0";
if(dlgFileOpen.DoModal()==IDOK)
{
Spath = (CString)dlgFileOpen.m_ofn.lpstrFile;
}
else
{
MessageBox("原备份文件名出错!",NULL,MB_ICONSTOP);
return;
}
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(Spath,
CFile::modeRead | CFile::shareDenyWrite, &ex))
{
SF.Close();
MessageBox("打开备份文件失败!");
return;
}
else
{
if (!DF.Open(ConAccPath, CFile::modeWrite |
CFile::shareExclusive | CFile::modeCreate, &ex))
{
MessageBox("打开当前数据库文件失败!");
DF.Close();
return;
}
BYTE buffer[4096];
DWORD dwRead;
do
{
dwRead = SF.Read(buffer, 4096);
DF.Write(buffer, dwRead);
}
while (dwRead > 0);
CString Msg; Msg.Format("Access数据库已还原到%s",ConAccPath);
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;
CString errMsg = e.ErrorMessage();
if (errMsg.Right(5)=="#3149")
errormessage.Format("数据库连接超时!\r\n请检查用户名和密码!");
else
errormessage.Format("数据库连接超时!\r\n请检查数据库状态!\r\n数据连接错误:%s",e.ErrorMessage());
MessageBox(errormessage,"连接出错",MB_OK|MB_ICONERROR);///显示错误信息
}
}
}
void CViewSys::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
CString ConSQL;
// m_pConnection->Open("driver={SQL Server};Server=hfx;DATABASE=StuGrade;UID=sa;PWD=604;","","",adModeUnknown);
ConSQL = pFrame->ConSQL;
int fd = ConSQL.Find("DATABASE=",0);
int fe = ConSQL.Find(";",fd);
int len =fe-fd-9;
DatabaseName = ConSQL.Mid(fd+9,len);
}
void CViewSys::OnCancel()
{
// TODO: Add your control notification handler code here
}
void CViewSys::OnSysManager()
{
MessageBox("还没有时间完成:)","保留功能嘻",MB_ICONINFORMATION);
}
void CViewSys::OnAddLogin()
{
// TODO: Add your control notification handler code here
int fc = pFrame->ConSQL.Find("SQL Server",0);
if (fc==-1) //当前连接不是SQL Server
{
MessageBox("在Access中不能新建用户");
return;
}
CString szSQL,user,pass;
CDLGaddlogin dlg;
dlg.m_Flg = 1;
dlg.m_pUser = &user;
dlg.m_pPass = &pass;
int ask = dlg.DoModal();
if (ask == IDOK)
{
//user = "hfx6"; pass = "hfx6";
szSQL.Format("EXEC sp_addlogin '%s', '%s', '%s'",user,pass,DatabaseName);
try {
pFrame->ExecSQL(szSQL);
CString Msg;
Msg.Format("添加SQL系统用户< %s >成功!请记住密码 %s 。",user,pass);
MessageBox(Msg,NULL,MB_ICONINFORMATION);
}
catch (_com_error e)
{
MessageBox("添加用户失败!",NULL,MB_ICONSTOP);
}
szSQL.Format("sp_adduser '%s','%s','db_owner'",user,user);
//szSQL.Format("GRANT ALL ON T_分科学分,专业信息,奖学金,学生基本信息,成绩表,权限管理,班级信息,系别信息,考试类型代码,课程信息 TO %s",user);
try {
pFrame->ExecSQL(szSQL);
CString Msg;
Msg.Format("数据库%s授权%s成功!",DatabaseName,user);
MessageBox(Msg,NULL,MB_ICONINFORMATION);
}
catch (_com_error e)
{
CString Msg;
Msg.Format("数据库%s授权%s失败!",DatabaseName,user);
MessageBox(Msg,NULL,MB_ICONSTOP);
}
}
}
void CViewSys::OnChangePass()
{
//取当前用户名
CString ConSQL,user,pass;
// m_pConnection->Open("driver={SQL Server};Server=hfx;DATABASE=StuGrade;UID=sa;PWD=604;","","",adModeUnknown);
ConSQL = pFrame->ConSQL;
int fd = ConSQL.Find("UID=",0);
int len =ConSQL.Find(";",fd)-fd-4;
user = ConSQL.Mid(fd+4,len);
if (ConSQL.Find("SQL Server",0)==-1)
user ="admin";
fd = ConSQL.Find("PWD=",0);
len = ConSQL.Find(";",fd)-fd-4;
pass = ConSQL.Mid(fd+4,len);
CDLGaddlogin dlg;
dlg.m_Flg = 0;
dlg.m_pUser = &user;
dlg.m_pPass = &pass;
if (dlg.DoModal()==IDOK)
{
ConSQL.Format("EXEC sp_password NULL, '%s', '%s'",pass,user);
try {
pFrame->ExecSQL(ConSQL);
CString Msg;
Msg.Format("更改%s用户密码成功!记住新密码%s。",user,pass);
MessageBox(Msg,NULL,MB_ICONINFORMATION);
int sp = pFrame->ConSQL.Find("PWD=",0);
sp+=4;
int len = pFrame->ConSQL.Find(";",sp)-sp;
// CString T_str = pFrame->ConSQL.Mid(sp,len);
pFrame->ConSQL.Delete(sp,len);
pFrame->ConSQL.Insert(sp,pass);
}
catch (_com_error e)
{
CString Msg;
Msg.Format("更改%s用户密码失败!",user);
MessageBox(Msg,NULL,MB_ICONSTOP);
}
}
}
void CViewSys::OnSysExituser()
{
// TODO: Add your command handler code here
if (pFrame->m_pConnection->State)
{
pFrame->m_pConnection->Close();
MessageBox("注销用户成功!","注销",MB_ICONASTERISK);
}
CString ConSQL;
CDlgLogin dlg;
dlg.ConSQL=&ConSQL;
if (dlg.DoModal()==IDCANCEL)
exit(0);
pFrame->ConSQL = ConSQL;
//重新打开连接
try{
pFrame->m_pConnection->ConnectionTimeout=5;
pFrame->m_pConnection->Open((LPCSTR)(ConSQL),"","",adModeUnknown);
MessageBox("用户登录成功!","重登录",MB_ICONINFORMATION);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("数据库连接超时!请检查数据库状态!\r\n数据连接错误:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
void CViewSys::OnUserLogin()
{
// TODO: Add your command handler code here
CString ConSQL;
CDlgLogin dlg;
dlg.ConSQL=&ConSQL;
if (dlg.DoModal()==IDCANCEL)
return;
//关闭原来连接
if (pFrame->m_pConnection->State)
pFrame->m_pConnection->Close();
pFrame->ConSQL = ConSQL;
//重新打开连接
try{
pFrame->m_pConnection->ConnectionTimeout=5;
pFrame->m_pConnection->Open((LPCSTR)(ConSQL),"","",adModeUnknown);
MessageBox("用户登录成功!","重登录",MB_ICONINFORMATION);
}
catch(_com_error e)
{
CString errormessage;
CString errMsg = e.ErrorMessage();
if (errMsg.Right(5)=="#3149")
errormessage.Format("数据库连接超时!\r\n请检查用户名和密码!");
else
errormessage.Format("数据库连接超时!\r\n请检查数据库状态!\r\n数据连接错误:%s",e.ErrorMessage());
MessageBox(errormessage,"连接出错",MB_OK|MB_ICONERROR);///显示错误信息
OnUserLogin();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -