⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dlglogin.cpp

📁 成绩管理系统
💻 CPP
字号:
// DlgLogin.cpp : implementation file
//

#include "stdafx.h"
#include "STUGRADE.h"
#include "DlgLogin.h"
#include "BrowseDirDialog.h"

//#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgLogin dialog

//extern CMainFrame *pFrame; 
CDlgLogin::CDlgLogin(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgLogin::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgLogin)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	
}


void CDlgLogin::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgLogin)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgLogin, CDialog)
	//{{AFX_MSG_MAP(CDlgLogin)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_CBN_SELENDOK(IDC_COMBO1, OnSelendokCombo1)
	ON_BN_CLICKED(IDC_test, Ontest)
	ON_BN_CLICKED(IDC_BUTTON3, OnRestore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgLogin message handlers

void CDlgLogin::OnOK() 
{
	CString constring;
    constring = GetConstring(1);
	if (strcmp(constring,"error"))
		*ConSQL=constring;
	else 
		return;
	CDialog::OnOK();
}

BOOL CDlgLogin::OnInitDialog() 
{
	CDialog::OnInitDialog();
     
	// TODO: Add extra initialization here
	CComboBox *pCombo = (CComboBox*) GetDlgItem(IDC_COMBO1);
	pCombo->SetCurSel(0);
    SetDlgItemText(IDC_EDIT_Server,"hfx");
	SetDlgItemText(IDC_EDIT_Database,"StuGrade");
	SetDlgItemText(IDC_EDIT_User,"sa");
	SetDlgItemText(IDC_EDIT_Pass,"");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgLogin::OnButton1() //定位数据库
{
    CFileDialog dlgFileOpen(TRUE);
	if(dlgFileOpen.DoModal()==IDOK) 
		SetDlgItemText(IDC_EDIT_Database,(CString)dlgFileOpen.m_ofn.lpstrFile);
	else 
	{
		MessageBox("打开文件出错!");
		return;
	}
}

void CDlgLogin::OnSelendokCombo1() 
{   
	CString ComoText;
	CComboBox *pCombo = (CComboBox*) GetDlgItem(IDC_COMBO1);
	pCombo->GetLBText(pCombo->GetCurSel(),ComoText);   //OK
    if (ComoText=="SQL Server数据库")
	{
		CEdit *pEdit1 = (CEdit*)GetDlgItem(IDC_EDIT_Server);
        pEdit1->EnableWindow(TRUE);
		CEdit *pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT_User);
        pEdit2->EnableWindow(TRUE);
        CButton *pBton = (CButton*)GetDlgItem(IDC_BUTTON1);
		pBton->ShowWindow(FALSE);
		SetDlgItemText(IDC_EDIT_Server,"hfx");
		SetDlgItemText(IDC_EDIT_Database,"StuGrade");
		SetDlgItemText(IDC_EDIT_User,"sa");
		SetDlgItemText(IDC_EDIT_Pass,"");			
	}
	else if (ComoText=="Access桌面数据库")
	{
		CEdit *pEdit1 = (CEdit*)GetDlgItem(IDC_EDIT_Server);
        pEdit1->EnableWindow(FALSE);
		CEdit *pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT_User);
        pEdit2->EnableWindow(FALSE);
        CButton *pBton = (CButton*)GetDlgItem(IDC_BUTTON1);
		pBton->ShowWindow(TRUE);
		SetDlgItemText(IDC_EDIT_Server,"");
		SetDlgItemText(IDC_EDIT_Database,"");
		SetDlgItemText(IDC_EDIT_User,"");
		SetDlgItemText(IDC_EDIT_Pass,"");
	}   	
}

void CDlgLogin::Ontest() 
{
	CString constring;
    constring = GetConstring(1);
	if (strcmp(constring,"error"))  //constring=="error"返回0
    {
		HRESULT hr; 
		hr = m_pConnection.CreateInstance(__uuidof( Connection )) ;        
        try{
			ASSERT(!FAILED(hr));
			if(SUCCEEDED(hr)) {
				m_pConnection->ConnectionTimeout=5;
				m_pConnection->Open((LPCSTR)constring,"","",adModeUnknown);
				MessageBox("数据库连接成功!","测试连接",MB_OK|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);///显示错误信息
		}
	}
}

CString CDlgLogin::GetConstring(int masterFlg )  //=0为取master数据库名
{
	CString ComoText,constring;
	CString servername,databasename,user,password;
	constring.Empty();
	GetDlgItemText(IDC_EDIT_Server,servername);// 取服务器名
	GetDlgItemText(IDC_EDIT_Database,databasename);
	GetDlgItemText(IDC_EDIT_User,user);
	GetDlgItemText(IDC_EDIT_Pass,password);
    if (databasename.IsEmpty())
	{ 
		MessageBox("数据库名不能为空!请确认输入","数据库名出错",MB_OK|MB_ICONINFORMATION);
		return "error";
	}
	CComboBox *pCombo = (CComboBox*) GetDlgItem(IDC_COMBO1);
	pCombo->GetLBText(pCombo->GetCurSel(),ComoText);   //OK
    if (ComoText=="SQL Server数据库")
	{
		if (servername.IsEmpty())
		{ 
			MessageBox("服务器名不能为空!请确认输入","服务器名出错",MB_OK|MB_ICONINFORMATION);
			return "error";
		}
		if (user.IsEmpty())
		{ 
			MessageBox("用户名不能为空!请确认输入","用户名出错",MB_OK|MB_ICONINFORMATION);
			return "error";
		}		
		if (masterFlg)
			constring.Format("driver={SQL Server};Server=%s;DATABASE=%s;UID=%s;PWD=%s;",servername,databasename,user,password);
		else 
			constring.Format("driver={SQL Server};Server=%s;DATABASE=master;UID=%s;PWD=%s;",servername,user,password);

		//	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);
	}
	else if (ComoText=="Access桌面数据库")
	{
		constring.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s",databasename);
	}
	return constring;
}

void CDlgLogin::OnRestore() 
{
	CString ComoText,ConSQL;
	CString databasename,user,password;
	
	GetDlgItemText(IDC_EDIT_Database,databasename);
	CComboBox *pCombo = (CComboBox*) GetDlgItem(IDC_COMBO1);
	pCombo->GetLBText(pCombo->GetCurSel(),ComoText);  
	
    if (ComoText=="SQL Server数据库")
	{
		ConSQL.Empty();
		ConSQL = GetConstring(0);  //取master连接
		if (!strcmp(ConSQL,"error"))  //constring=="error"返回0
			return;
		HRESULT hr;	
		hr = m_pConnection.CreateInstance(__uuidof( Connection )) ;        
        ASSERT(!FAILED(hr));
		try{
			m_pConnection->ConnectionTimeout=5;
			hr =  m_pConnection->Open((LPCSTR)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);///显示错误信息
		    return;
		}
		//CString szSQL;
		BOOL move=false;
		char *movepath = new char[255];
		movepath[0] = '\0';
		CFileDialog dlgFileOpen(TRUE),dataFileSave(FALSE);
		dlgFileOpen.m_ofn.lpstrTitle = "选择备份文件";
		dlgFileOpen.m_ofn.lpstrFilter ="所有文件 (*.*)\0*.*;\0DAT文件 (*.dat)\0*.dat\0";
	if(dlgFileOpen.DoModal()==IDOK) 	
	{
remove:
		char *szSQL = new char[255];
		szSQL[0] = '\0';
		strcat(szSQL,"RESTORE DATABASE ");
		strcat(szSQL,databasename);
		strcat(szSQL," FROM DISK = '");
		strcat(szSQL,dlgFileOpen.m_ofn.lpstrFile);
		strcat(szSQL,"'");
		if (move)
		{
			strcat(szSQL," with move 'StuGrade_Data' TO '");
			strcat(szSQL,movepath);
			strcat(szSQL,"\\StuGrade_Data.MDF', move 'StuGrade_Log' TO '");
			strcat(szSQL,movepath);
			strcat(szSQL,"\\StuGrade_Log.LDF'");
		}
		//szSQL.Format("RESTORE DATABASE s FROM DISK = '%s' WITH REPLACE",(CString)T_DBname,(CString)dlgFileOpen.m_ofn.lpstrFile);
		//MessageBox(szSQL);
		try{
			m_pConnection->Execute(szSQL,NULL,adExecuteNoRecords);
			CString Msg;
			Msg.Format("成功还原数据库为:%s!",databasename);
			MessageBox(Msg,"还原成功",MB_ICONINFORMATION);
		}
		catch (_com_error e)
		{
			char *Msg = new char[255];  Msg[0] = '\0';
			char *ss = "恢复出错,错误代码:";
			strcat(Msg,ss); 
			strcat(Msg,e.ErrorMessage()); //错误:3092
			strcat(Msg,"\r\n要新建个SQL server数据库吗?");
			int ask = MessageBox(Msg,"新建数据库",MB_YESNO|MB_ICONSTOP);
			if (ask == IDYES)
			{	
				move = true;			   
				CBrowseDirDialog Dirdlg;
				Dirdlg.m_Title="小猪快跑!";
				if (Dirdlg.DoBrowse()!=0)
				{   //  MessageBox(Dirdlg.m_Path);
					strcpy(movepath,Dirdlg.m_Path);
					goto remove;
				}
			}
		}//end catch ()	
		}  //end openfile		
	}
	else //当前为Access连接 还原
	{
		CString Spath,Dpath;;
		CFileDialog dlgFileOpen(TRUE),dlgFileSave(FALSE);
		dlgFileOpen.m_ofn.lpstrTitle = "打开MDB数据库备份";
		dlgFileOpen.m_ofn.lpstrFilter ="Access数据文件 (*.mdb)\0*.mdb;\0所有文件 (*.*)\0*.*\0";
		dlgFileSave.m_ofn.lpstrTitle = "MDB数据库还原为";
		dlgFileSave.m_ofn.lpstrFilter ="Access数据文件 (*.mdb)\0*.mdb;\0所有文件 (*.*)\0*.*\0";
				
		if(dlgFileOpen.DoModal()==IDOK) 
		{
			Spath = (CString)dlgFileOpen.m_ofn.lpstrFile;	
		}
		else 
		{
		//	MessageBox("原备份文件名出错!",NULL,MB_ICONSTOP);
			return;
		}	
		if(dlgFileSave.DoModal()==IDOK) 
		{
			Dpath = (CString)dlgFileSave.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;
		}
		//ConAccPath = "H:\\dos.chm";
	
		CFile SF,DF;
		CFileException ex;		
		if (!SF.Open(Spath,
			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;
			do
			{
				dwRead = SF.Read(buffer, 4096);
				DF.Write(buffer, dwRead);
			}
			while (dwRead > 0);	

			SetDlgItemText(IDC_EDIT_Database,Dpath);

			CString Msg; Msg.Format("Access数据库已还原到%s",Dpath);
			MessageBox(Msg,"备份成功!",MB_ICONINFORMATION);
			// Close both files
			SF.Close();
			DF.Close();
		}//end if(!SF.open())
	
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -