C++下取出SQL Server 数据库数据

//声明一个智能指针
_ConnectionPtr m_pConnectionPtr;
//创建ConnectionPtr 实例
HRESULT hr = m_pConnectionPtr.CreateInstance("ADODB Connection");

_bstr_t strConnect = "Provider=SQLOLEDB.1;Password=passwd;Persist Security Info=True;User ID=sa;Initial Catalog=INGRAN_DB;Data Source=192.168.253.129,1433";
m_pConnectionPtr->Open(strConnect, "", "", adModeUnknown);//连接数据库



_RecordsetPtr m_pRecordset;//创建记录集
m_pRecordset.CreateInstance(_uuidof(Recordset));//创建m_pRecordset实例

_bstr_t strSQL = "select * from Orders";//sql语句
m_pRecordset = m_pConnectionPtr->Execute(strSQL, NULL, adCmdText);//m_pConnectionPtr返回结果集
while (!m_pRecordset->adoEOF)
{
    _variant_t vcustomer = m_pRecordset->GetCollect("customer"); //获取表Orders中的列customer中的第一个值
    _variant_t vPrice = m_pRecordset->GetCollect("price");
    std::cout << "Customer:" << (char *)(_bstr_t)vcustomer << " Price:" << vPrice.intVal << std::endl;
    m_pRecordset->MoveNext();//将记录集指向下一个
}


#if 1 增删改 利用m_pRecordset
    //m_pRecordset->Open("insert into Orders values(7,100,'2018-11-7','ccc')", m_pConnectionPtr.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);  //增加  //这条语句执行后会自动关闭m_pRecordset ,代码结尾处需要判定

    //m_pRecordset->Open("update Orders set customer='ddd' where customer='ccc'", m_pConnectionPtr.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);//修改

    m_pRecordset->Open("delete from Orders where customer='ddd'", m_pConnectionPtr.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);//删除

#endif