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

📄 devicedbs.cpp

📁 < Visual C+++SQL Server数据库开发与实例>>一书中的实例核心源程序提供了几个案例的编程源代码。
💻 CPP
字号:
// DeviceDBS.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "DeviceDBS.h"
#include "DeviceDBSDlg.h"
#include "LoginDlg.h"
#include "User.h"
#include "PowerHelper.h"

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


CDatabase m_db;
User user;
PowerHelper helper;
/////////////////////////////////////////////////////////////////////////////
// CDeviceDBSApp

BEGIN_MESSAGE_MAP(CDeviceDBSApp, CWinApp)
	//{{AFX_MSG_MAP(CDeviceDBSApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDeviceDBSApp construction

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

/////////////////////////////////////////////////////////////////////////////
// The one and only CDeviceDBSApp object

CDeviceDBSApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDeviceDBSApp initialization

BOOL CDeviceDBSApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CDeviceDBSDlg dlg;
	CLoginDlg login;
	initDB();

	if(login.DoModal()!=IDOK) return FALSE;

	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
void CDeviceDBSApp::initDB(){

	char szPath[255];
	//获取应用程序完全路径
	::GetModuleFileName(NULL,szPath,255);
	CString strFileName = szPath;
	//获取所在的目录名称
	strFileName.Delete(strFileName.ReverseFind('\\')+1,strFileName.GetLength ()-strFileName.ReverseFind('\\')-1);
    //构造配置文件的完全路径
	strFileName += "DeviceDBS.ini";
	TCHAR sz[101];
	memset(sz,0,sizeof(TCHAR)*101);
	//获取配置文件中数据库数据源的值,如果没有,缺省值为device
	GetPrivateProfileString(_T("General"),_T("数据库数据源"),_T("device"),sz,100,strFileName);
	CString strSource(sz);
	//AfxMessageBox("datasource "+strSource);
	GetPrivateProfileString(_T("General"),_T("数据库用户"),_T("dbaccess"),sz,100,strFileName);
	CString strUser(sz);
	//AfxMessageBox("user "+strUser);
	GetPrivateProfileString(_T("General"),_T("数据库密码"),_T("dbaccess"),sz,100,strFileName);
	CString strPwd(sz);	
	//AfxMessageBox("password "+strPwd);
	//创建连接字符串.
	CString strConnect;
	strConnect.Format("DSN=%s;UID=%s;PWD=%s",strSource,strUser,strPwd);
	//打开数据库的连接,并且捕获异常
	TRY{
		m_db.OpenEx(strConnect,CDatabase::noOdbcDialog);
	}
	CATCH(CDBException,ex)
	{
		AfxMessageBox (ex->m_strError);
		AfxMessageBox (ex->m_strStateNativeOrigin);		
	}
	AND_CATCH(CMemoryException,pEx)
	{
		pEx->ReportError();
		AfxMessageBox ("memory exception");
	}
	AND_CATCH(CException,e)
	{
		TCHAR szError[100];
		e->GetErrorMessage(szError,100);
		AfxMessageBox (szError);
	}
	END_CATCH	
}

⌨️ 快捷键说明

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