connectdb.cpp

来自「数理统计类软件」· C++ 代码 · 共 67 行

CPP
67
字号
// ConnectDB.cpp: implementation of the CConnectDB class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SPSS.h"
#include "ConnectDB.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CConnectDB::CConnectDB()
{
	TRY
	{
//		m_db.OpenEx("DSN=fanglei;", CDatabase::noOdbcDialog );
		CString sDriver = "Microsoft Access Driver (*.mdb)";
		CString sFile = GetExeDictionary()+"Data.mdb"; 
		CString sDsn;
		sDsn.Format("DRIVER={%s};DBQ=%s",sDriver,sFile);

		m_db.OpenEx(sDsn);
	}
	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
//	m_pRecordset = new CRecordset(&m_db);

}

CConnectDB::~CConnectDB()
{
	m_db.Close();
}

CString CConnectDB::GetExeDictionary()
{
	
	//获得当前程序目录(.exe)
	TCHAR szFilePath[MAX_PATH+1];
	GetModuleFileName(NULL, szFilePath, MAX_PATH);
	(_tcsrchr(szFilePath, _T('\\')))[1] = 0;
	CString str_url = szFilePath;
	return str_url;
}

⌨️ 快捷键说明

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