VC怎样查询access数据库中某个数据
1个回答
2017-04-16
展开全部
VC怎样查询access数据库中某个数据
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;
}
二、使用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;
}
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;
}
二、使用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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询