文章目录
- 导入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库
}