<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif;">【why】文件处理在安全级别和随机读取能力以及查询方便等各方面无法满足我们的需要,因此我们需要在MFC程序中引入数据库,引入数据库常通过ODBC和ADO两种方式,引入ODBC只需要进行一些基本设置即可,这里给出ADO的一些基本代码模板</span>
【how】
1.引入ADO动态链接库
在StdAfx.h中加入
#import "C:\program files\common files\System\ado\msado15.dll" no_namespace \
rename("EOF","adoEOF") \
rename("LockTypeEnum","newLockTypeEnum")\
rename("DataTypeEnum","newDataTypeEnum")\
rename("FieldAttributeEnum","newFieldAttributeEnum")\
rename("EditModeEnum","newEditModeEnum")\
rename("RecordStatusEnum","newRecordStatusEnum")\
rename("ParameterDirectionEnum","newParameterDirectionEnum")
根据机器配置不同,可能会有相应的更改
2.定义智能指针对象
在主类的头文件的主类中增加两个成员变量
public:
_RecordsetPtr m_pRs;
private:
_ConnectionPtr m_pConn;
这里要说明的是
_RecordsetPtr智能指针,它是专门为通过记录集操作数据库而设立的指针,通过该接口可以对数据库的表内的记录、字段等进行各种操作。
要搞清楚的是数据库和ADO的记录集是两个不同的概念, 是存在于不同物理位置的两个存储空间。 记录集相当于是实际数据的一份拷贝。 记录集是相对脱离数据库而存在的。
3.初始化智能指针
在主类的cpp文件中实现InitInstance函数初始化 这里以类CGIS为例
BOOL CGISApp::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
// Create ADO Connection
if( FAILED(::CoInitialize(NULL)) )
{
AfxMessageBox("ADO Init failed");
return false;
}
try
{
m_pConn.CreateInstance(__uuidof(Connection));
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=GIS.mdb","","",adConnectUnspecified);//这里GIS.mdb是和程序在同一目录的数据库
}
// Catch Exceptions
catch(_com_error &e)
{
CString err;
err.Format("%s", (char*)(e.Description()) );
AfxMessageBox(err);
}
catch(...)
{
AfxMessageBox("Unknown Error...");
}
// Init ADO RecordSet
m_pRs.CreateInstance(__uuidof(Recordset));
CGISDlg dlg;
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;
}
注意:
这里GIS.mdb是和程序在同一目录的数据库
4.统一接口ADOExecute的实现
依旧以类CGIS为例,在头文件的类中定义成员函数
bool ADOExecute(_RecordsetPtr &ADOSet, _variant_t &strSQL);
然后在cpp文件中实现该函数
bool CGISApp::ADOExecute(_RecordsetPtr &ADOSet, _variant_t &strSQL)
{
if ( ADOSet->State == adStateOpen)
ADOSet->Close();
try
{
ADOSet->Open(strSQL, m_pConn.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdUnknown);
return true;
}
catch(_com_error &e)
{
CString err;
err.Format("ADO Error: %s",(char*)e.Description());
AfxMessageBox(err);
return false;
}
}
至此,ADO数据库就成功引入程序中,每次使用时调用主类实例的ADOExecute 传入的参数为智能指针和sql语句 m_prs即指向记录集
【postscript】
这里给出一个大牛的相关文章 内容更加丰富
http://blog.youkuaiyun.com/fengzhishang2019/article/details/7890041