www.gusucode.com > VC++ Access数据库通用权限管理模源码程序 > VC++ Access数据库通用权限管理模源码程序/code/PowerSet.cpp

    //Download by http://www.NewXing.com
// PowerSet.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "PowerSet.h"
#include "MainFrm.h"
#include "PowerSetDoc.h"
#include "PowerSetView.h"
#include "LoginDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPowerSetApp

BEGIN_MESSAGE_MAP(CPowerSetApp, CWinApp)
	//{{AFX_MSG_MAP(CPowerSetApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPowerSetApp construction

CPowerSetApp::CPowerSetApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CPowerSetApp object

CPowerSetApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CPowerSetApp initialization

BOOL CPowerSetApp::InitInstance()
{
	AfxEnableControlContainer();
    
    /////////////////////////////////////////////////////////
	//进行COM初始化
	//作者:武
	//////////////////////////////////////////////////////////
	if(!AfxOleInit())
	{
		AfxMessageBox("OLE初始化错误!");
		return FALSE;
	}
    /////////////////////////

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif
	/////////////////////////////////////////////////////////
	//连接access2000数据库。
	//作者:武
	//////////////////////////////////////////////////////////
	HRESULT hr;
	try
	{
		hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象

		if(SUCCEEDED(hr))
		{
			m_pConnection->ConnectionTimeout=5;///设置超时时间为3秒
         //	hr = m_pConnection->Open(Filepath,"","",adModeUnknown);
			hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=manager.mdb;","","",adModeUnknown);
			///连接数据库
			///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;  }
		}
	}
	catch(_com_error e)///捕捉异常
	{
		CString temp;
		temp.Format("连接数据库错误信息:%s",e.ErrorMessage());
		::MessageBox(NULL,temp,"提示信息",NULL);
		return false;
	}


	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CPowerSetDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CPowerSetView));
	AddDocTemplate(pDocTemplate);

	/////////////////////////////////////////////////////////
	//用户登录,进行权限验证
	//作者:武
	//////////////////////////////////////////////////////////
	CLoginDlg logdlg;
	if(logdlg.DoModal()==IDOK)
	{
	  CString sql="SELECT * FROM USERS where username='"+theApp.name+"'";
	}
	else
	{
	  return FALSE;
	}
   //////////////////////////////////////////////
	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	CString title="通用权限设置模块演示";	
	m_pMainWnd->SetWindowText(title);
	
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// 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)
		// No message handlers
	//}}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()

// App command to run the dialog
void CPowerSetApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CPowerSetApp message handlers


int CPowerSetApp::ExitInstance() 
{  
	///如果已经打开了连接则关闭它
	if(m_pConnection->State)
	{
		m_pConnection->Close(); 
	}
	
	return CWinApp::ExitInstance();
}