Windows平台的数据接口标准有ODBC、OLE DB、ADO和Borland的BDE接口,ODBC(Open DataBase Connectivity)只能用于访问关系型数据库,为了访问非关系型数据微软设计了OLE DB接口并在此基础上推出了ADO(ActiveX Data Objects)。本文介绍的方法是ODBC和ADO。
一、使用ODBC接口,在这里我们使用MFC的CDatabase类,该类是对SQLConnect等ODBC的API的封装。需要包含afxdb.h
BOOL ODBCConnect(CString strDBFile)
{
CString strConnect;
strConnect.Format(_T( " ODBC;DRIVER={MICROSOFT ACCESS DRIVER (*.mdb)};UID=sa;PWD=;DBQ=%s " ), strDBFile);
CDatabase db;
if (db.Open(NULL, FALSE, FALSE, strConnect))
{
// 连接数据库成功
CRecordset rs( & db);
CString strSql;
strSql = _T( " select * from info " ); // SQL语句
rs.Open(AFX_DB_USE_DEFAULT_TYPE, strSql); // 执行Sql语句(可添加 删除 查询等)
if (rs.IsOpen())
{
CDBVariant variant;
rs.MoveFirst();
while ( ! rs.IsEOF())
{
// 读取记录
rs.GetFieldValue(_T( " 姓名 " ), variant);
rs.MoveNext();
}
}
db.Close();
return TRUE;
}
return FALSE;
}
{
CString strConnect;
strConnect.Format(_T( " ODBC;DRIVER={MICROSOFT ACCESS DRIVER (*.mdb)};UID=sa;PWD=;DBQ=%s " ), strDBFile);
CDatabase db;
if (db.Open(NULL, FALSE, FALSE, strConnect))
{
// 连接数据库成功
CRecordset rs( & db);
CString strSql;
strSql = _T( " select * from info " ); // SQL语句
rs.Open(AFX_DB_USE_DEFAULT_TYPE, strSql); // 执行Sql语句(可添加 删除 查询等)
if (rs.IsOpen())
{
CDBVariant variant;
rs.MoveFirst();
while ( ! rs.IsEOF())
{
// 读取记录
rs.GetFieldValue(_T( " 姓名 " ), variant);
rs.MoveNext();
}
}
db.Close();
return TRUE;
}
return FALSE;
}
二、使用ADO连接数据库,因为要使用COM,需要初始化(CoInitialize).然后就可以生成接口的对象操作,代码如下:
#import
"
C:Program Files\Common Files\System\ado\msado15.dll
"
rename(
"
EOF
"
,
"
adoEOF
"
)
//
生成C++类,改变EOF函数的名称
using namespace ADODB;
BOOL ADOConnect(CString strDBFile)
{
_ConnectionPtr pConnection;
if (pConnection.CreateInstance(__uuidof(Connection)) != S_OK)
{
return FALSE;
}
CString strConnect;
strConnect.Format(_T( " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s " ), strDBFile);
if (pConnection -> Open(_bstr_t(strConnect), "" , "" , adModeUnknown) == S_OK)
{
// 连接数据库成功
_RecordsetPtr pRecordset;
if (pRecordset.CreateInstance(__uuidof(Recordset)) != S_OK)
{
pConnection -> Close();
return FALSE;
}
CString strSql;
strSql = _T( " select * from info " );
HRESULT hr = pRecordset -> Open(_bstr_t(strSql), _variant_t((IDispatch * )pConnection, TRUE), adOpenUnspecified, adLockUnspecified, adCmdUnknown);
if (hr != S_OK)
{
pConnection -> Close();
return FALSE;
}
_variant_t vt;
pRecordset -> MoveFirst();
while ( ! pRecordset -> adoEOF)
{
vt = pRecordset -> Fields -> GetItem( " 姓名 " ) -> Value;
pRecordset -> MoveNext();
}
pConnection -> Close();
return TRUE;
}
return FALSE;
}
using namespace ADODB;
BOOL ADOConnect(CString strDBFile)
{
_ConnectionPtr pConnection;
if (pConnection.CreateInstance(__uuidof(Connection)) != S_OK)
{
return FALSE;
}
CString strConnect;
strConnect.Format(_T( " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s " ), strDBFile);
if (pConnection -> Open(_bstr_t(strConnect), "" , "" , adModeUnknown) == S_OK)
{
// 连接数据库成功
_RecordsetPtr pRecordset;
if (pRecordset.CreateInstance(__uuidof(Recordset)) != S_OK)
{
pConnection -> Close();
return FALSE;
}
CString strSql;
strSql = _T( " select * from info " );
HRESULT hr = pRecordset -> Open(_bstr_t(strSql), _variant_t((IDispatch * )pConnection, TRUE), adOpenUnspecified, adLockUnspecified, adCmdUnknown);
if (hr != S_OK)
{
pConnection -> Close();
return FALSE;
}
_variant_t vt;
pRecordset -> MoveFirst();
while ( ! pRecordset -> adoEOF)
{
vt = pRecordset -> Fields -> GetItem( " 姓名 " ) -> Value;
pRecordset -> MoveNext();
}
pConnection -> Close();
return TRUE;
}
return FALSE;
}