stu2dlg.cpp

来自「成绩管理系统」· C++ 代码 · 共 388 行

CPP
388
字号
// stu2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "stu2.h"
#include "stu2Dlg.h"
#include "BrowseDirDialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStu2Dlg dialog

CStu2Dlg::CStu2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CStu2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStu2Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

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

BEGIN_MESSAGE_MAP(CStu2Dlg, CDialog)
	//{{AFX_MSG_MAP(CStu2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_backup, Onbackup)
	ON_BN_CLICKED(IDC_restore, Onrestore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStu2Dlg message handlers

BOOL CStu2Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	char str[255],strfile[255];
	GetCurrentDirectory(255,str);
	strcpy(strfile,str);			
	strcat(strfile,"\\restore.ini");
	strcat(str,"\\STUGRADE.exe");
	char ConSQLfile[255];
    CString ConSQL,rsDBname;

	CFile file;
	CFileException ex;		
	if (!file.Open(strfile,
		CFile::modeRead | CFile::shareDenyWrite,&ex))
	{
		MessageBox("我不是主程序,不要运行我哦:)!","恢复出错",MB_ICONINFORMATION);
	//	file.Close();
		exit(0);
	}
	file.Read(ConSQLfile,file.GetLength());
	CString Ts;
	strcpy ((LPSTR)(LPCSTR)Ts,ConSQLfile);	
	int fc = Ts.Find("@",0); 
	int fd = Ts.Find("#",0);
	if ((fc == -1) || (fd == -1))
	{	
    	MessageBox("恢复失败!","恢复出错",MB_ICONSTOP);
		exit(0);
	}	 
	fc++;
	int len =fd-fc;  //rsDBname	
	char *T_DBname = new char[len+1];
	char *T_ConSQL = new char[fc+1];
	for (int i=0;i<len;i++,fc++)
	{
        T_DBname[i] = ConSQLfile[fc];
	}
       T_DBname[len] = '\0';
    for (int j=0;j<fc-i-1;j++)
	{
        T_ConSQL[j] = ConSQLfile[j];
	}
     T_ConSQL[j] = '\0'; 
	file.Close();
	//保密起见,把restore.ini删除
	TRY
	{
		CFile::Remove(strfile);
	}
	CATCH( CFileException, e )
	{
#ifdef _DEBUG
		afxDump << "File " << strfile << " cannot be removed\n";
#endif
	}
	END_CATCH	
	HRESULT hr;	
	try{
		hr = m_pConnection_stu.CreateInstance(__uuidof( Connection )) ;
		//  hr = m_pConnection_stu->Open("driver={SQL Server};Server=hfx;DATABASE=STUGRADE;UID=sa;PWD=;","","",adModeUnknown);
		hr = m_pConnection_ma.CreateInstance(__uuidof( Connection )) ;
		m_pConnection_ma->ConnectionTimeout=5;
		hr =  m_pConnection_ma->Open(T_ConSQL,"","",adModeUnknown);
		// hr = m_pConnection_ma->Open("driver={SQL Server};Server=hfx;DATABASE=master;UID=sa;PWD=;","","",adModeUnknown);
		//ASSERT(!FAILED(hr));
	}
	catch (_com_error e)
	{
		MessageBox("数据连接超时!请检查数据库状态。","连接出错",MB_ICONSTOP);
		exit(0);
	}
	//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) 	
	{
		//szSQL = "CREATE TABLE [dbo].[何发贤] ([学号] [nvarchar] (16) COLLATE Chinese_PRC_CI_AS NULL,[课程名称] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,	[课程学分] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL )";
remove:
	   char *szSQL = new char[255];
	   szSQL[0] = '\0';
       strcat(szSQL,"RESTORE DATABASE ");
	  //strcat(szSQL,"aa");
       strcat(szSQL,T_DBname);
       strcat(szSQL," FROM DISK = '");
	   strcat(szSQL,dlgFileOpen.m_ofn.lpstrFile);
	   strcat(szSQL,"'");
	   //MessageBox(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{
			char *Msg = new char[255];  Msg[0] = '\0';
			strcat(Msg,"成功还原数据库为:"); 
		    strcat(Msg,T_DBname); 
			m_pConnection_ma->Execute(szSQL,NULL,adExecuteNoRecords);
        	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\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;
			   }
			   /*
			   if(dataFileSave.DoModal()==IDOK)
			   {
                   move=true;
				   char *temp = new char[255];
	               strcpy (temp,dataFileSave.m_ofn.lpstrFile);
				   for (int i=0;temp[i]!='\0';i++);
				   int j=-1;
				   if (temp[i-2]=='\\')
					   strcpy(movepath,dataFileSave.m_ofn.lpstrFile);
				   else
				   {
					   for (int i=0;temp[i]!='\0';i++)
					   {
						   if (temp[i]=='\\') j=i;  //j保存最后一个\号
					   }
					   if (j==-1) 
						   exit(0);
					   else  //把\后的全删除掉
					   {
						   temp[j+1]='\0';
					   }						   
				   }
			   }*/
		   }
		}//end catch ()	
	}  //end openfile
//	AfxMessageBox(str);
	try {
		ShellExecute(NULL,"open",str,0,0,SW_SHOW);
	}
	catch (CException e)
	{
        
	}
	exit(0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CStu2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CStu2Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CStu2Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CStu2Dlg::Onbackup() 
{
	// TODO: Add your control notification handler code here
	CString szSQL;
	CFileDialog dlgFileOpen(FALSE);
	if(dlgFileOpen.DoModal()==IDOK) 	
	{
		//szSQL = "CREATE TABLE [dbo].[何发贤] ([学号] [nvarchar] (16) COLLATE Chinese_PRC_CI_AS NULL,[课程名称] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,	[课程学分] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL )";
		szSQL.Format("BACKUP DATABASE StuGrade to DISK = '%s'",(CString)dlgFileOpen.m_ofn.lpstrFile);
		try{
			m_pConnection_ma->Execute((LPCSTR)szSQL,NULL,adExecuteNoRecords);
			MessageBox("备份成功!",NULL,MB_ICONINFORMATION);
		}
		catch (_com_error e)
		{
			MessageBox("备份出错!",NULL,MB_ICONSTOP);
		}
	}
}

void CStu2Dlg::Onrestore() //注意还原时候不能打开StuGrade数据的连接!!
{
	// TODO: Add your control notification handler code here
  if(m_pConnection_stu->State)
     m_pConnection_stu->Close();
   //      m_pConnection_stu->Close();
   //delete m_pConnection_stu;
  m_pConnection_stu.Release();
  m_pConnection_stu.Detach();

	CString szSQL;
	CFileDialog dlgFileOpen(TRUE);
	if(dlgFileOpen.DoModal()==IDOK) 	
	{
		//szSQL = "CREATE TABLE [dbo].[何发贤] ([学号] [nvarchar] (16) COLLATE Chinese_PRC_CI_AS NULL,[课程名称] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,	[课程学分] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL )";
		szSQL.Format("RESTORE DATABASE StuGrade FROM DISK = '%s'",(CString)dlgFileOpen.m_ofn.lpstrFile);
		try{
			m_pConnection_ma->Execute((LPCSTR)szSQL,NULL,adExecuteNoRecords);
			MessageBox("还原成功!",NULL,MB_ICONINFORMATION);
		}
		catch (_com_error e)
		{
			MessageBox("还原出错!",NULL,MB_ICONSTOP);
		}
	}
}

void CStu2Dlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

⌨️ 快捷键说明

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