1. 在stdafx.h最后面加上
#import "C:\Program Files\CommonFiles\System\ado\msado15.dll" \
no_namespace rename("EOF", "adoEOF")rename("BOF","adoBOF")
2. 在TestADODlg.h中添加
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
3. 在TestADODlg.cpp中的OnInitDialog中添加
if (!AfxOleInit()) { MessageBox(_T("数据库初始化错误!")); }
try { m_pConnection.CreateInstance(__uuidof(Connection));
_bstr_t strConnect = "Driver={SQLServer};Provider=SQLOLEDB;DSN=hotel;Server=192.168.2.28;Database=TalentVideoDB;UID=sa;PWD=tlserver;"; //其中的Server指定MSSQL2005安装所在的主机IP地址 m_pConnection->Open(strConnect, "", "", adModeUnknown); } catch(_com_error e) { MessageBox(e.Description()); }
try { m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open("SELECT * FROM dbo.DEV_Encoders", m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error e) { MessageBox(e.Description()); }
_variant_t theValue; CString strTmp;
while (!m_pRecordset->adoEOF) { theValue = m_pRecordset->GetCollect(_T("EncoderName")); strTmp = (char *)_bstr_t(theValue);//转为CString类型
m_pRecordset->MoveNext(); }
|