在vs2010中用怎样的c++语句访问数据库

2025-01-02 22:51:23
推荐回答(1个)
回答1:

不好意思,回答错了,我看成是另一个问题了!
有多种方法访问数据库:
ADO方式或OLEDB,给你个OLEDB范例吧

BOOL CDataTableFactory::GetCompDataTable( CEnterpriseInfoVector* pRetVector, LPTSTR strQuery )
{

CDataSource db;
CSession session;
HRESULT hr;
CCommand, CRowset, CMultipleResults > command;

hr = db.OpenFromInitializationString( m_strConnection );

ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return FALSE;
hr = session.Open(db);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return FALSE;

hr = command.Open(session, strQuery);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return FALSE;

while (command.MoveNext() == S_OK)
{
auto_ptr item( new CCompanyInfoBase(static_cast(command)) );
item->CurrentState = DataEntity::RSINITIAL;
pRetVector->Add( item.release() );
}

command.Close();
session.Close();
db.Close();

return TRUE;
}