📄 database.cpp
字号:
#include "stdafx.h"
#include "SalaryManagement.h"
#include "DataBase.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
void CDataBase::InitConnection(){ //初始化数据库连接
::CoInitialize(NULL); //初始化COM环境
try{
m_pConnection.CreateInstance(__uuidof(Connection)); //创建Connection的对象
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\DataBase\\salary.mdb","","",adModeUnknown);
} //不用ODBC源连接数据库
catch(_com_error e){ //捕捉数据库连接时的异常
AfxMessageBox(e.Description()); //给出错误信息
}
}
void CDataBase::GetRecordset(CString SQL){ //查询
_bstr_t sql=SQL; //得到能进行查询的SQL语句
try{
m_pRecordset.CreateInstance(__uuidof(Recordset)); //创建Recordset的对象
m_pRecordset=m_pConnection->Execute(sql,NULL,adCmdText);
}
catch(_com_error e){ //捕捉查询时的异常
AfxMessageBox(e.Description()); //给出错误信息
}
}
void CDataBase::ExecuteSQL(CString SQL){ //执行SQL语句
_bstr_t sql=SQL; //得到要执行的SQL语句
try{
m_pConnection->Execute(sql,NULL,adCmdText);
}
catch(_com_error e){ //捕捉查询时的异常
AfxMessageBox(e.Description()); //给出错误信息
}
}
void CDataBase::ExitConnection(){ //退出数据库连接
m_pConnection->Close(); //断开连接
if(m_pRecordset!=NULL)
m_pRecordset->Close();
::CoUninitialize(); //释放COM环境
}
CDataBase::CDataBase(){
}
CDataBase::~CDataBase(){
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -