文章目录
- 导入ADO库
导入ADO库
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")
void CADODlg::OnBnClickedBtnQuery()
{
//导入ADO库
::CoInitialize(NULL);//初始化COM库
_ConnectionPtr pConn(_uuidof(Connection));//_ConnectionPtr为智能指针,_uuidof全局唯一标识符
_RecordsetPtr pRst(__uuidof(Recordset));//记录及智能指针对象
pConn->ConnectionString = "";//连接字符串
pConn->Open("","mysql","123456",adConnectUnspecified);//打开数据库的连接,adConnectUnspecified同步打开连接
pRst = pConn->Execute("Select* from authors", NULL, adCmdText);
while (!pRst->rsEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("au_lname"));//查询姓
pRst->MoveNext();
}
pRst->Close();//关闭智能指针
pConn->Close();//关闭连接
pRst.Release();
pConn.Release();
CoUninitialize();//卸载COM库
}