这是按照孙鑫C++视频第二十讲编写的,但是还没有在VS2012中找到如何得到ConnectionString的方法,待解决,多样数据库的连接
void CAdoDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CoInitialize(NULL) ;//初始Com库
_ConnectionPtr pConn(__uuidof(Connection)) ;//__uuidof()获取Connection全局唯一标示符
_RecordsetPtr pRst(__uuidof(Recordset)) ;
_CommandPtr pCmd(__uuidof(Command)) ;
pConn->ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=pubs" ;//在此添加链接字符串的语句
pConn->Open("" , "" , "" , adConnectUnspecified) ;//查阅MSDN了解相关参数
pRst = pConn->Execute("select * from authors" , NULL , adCmdText) ;
pRst->Open("select * from authors" , _variant_t((IDispatch *)pConn) ,
adOpenDynamic , adLockOptimistic , adCmdText) ;
pCmd->put_ActiveConnection(_variant_t((IDispatch *)pConn)) ;
pCmd->CommandText = "select * from authors" ;
while (!pRst->rsEOF)
{
((CListBox *)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("au_lname")) ;
pRst->MoveNext() ;
}
pRst->Close() ;
pConn->Close() ;
pCmd->Release() ;
pCmd->Release() ;
pRst->Release() ;
pConn->Release() ;
CoUninitialize() ;
}