本人按照孙鑫老师的第20个专题“数据库访问”写的程序,程序如下。
说明:yerou为SQL数据库名,TABLE1是里面的表,BB是表中的一个列名。
///////////////////////////////////////////////////////////////////
void CAdoDlg::OnBtnQuery() 
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
_CommandPtr pCmd(__uuidof(Command)); pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=yerou";
pConn->Open("","","",adConnectUnspecified); pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
pCmd->CommandText="select * from yerou";
pRst=pCmd->Execute(NULL,NULL,adCmdText);
while(!pRst->rsEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("BB"));
pRst->MoveNext();
}

pRst->Close();
pConn->Close();
pCmd.Release();
pRst.Release();
pConn.Release();
CoUninitialize();
}
////////////////////////////////////////////////////////////////////
运行时出现错误,提示为:
Runtime Error!
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
////////////////////////////////////////////////////////////////////
请高手指点!