Code
// adotest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#import "c:\Windows\System32\msado15.dll" no_namespace rename("EOF","adoEOF")
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
_ConnectionPtr m_pConnection;
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(!SUCCEEDED(hr))
{
std::cout<<"m_pConnection.CreateInstance(__uuidof(Connection)) error"<<std::endl;
Sleep(-1);
return 0;
}
std::string str = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Sell;Data Source=WUBIYU-PC";
m_pConnection->ConnectionString ="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Sell;Data Source=WUBIYU-PC" ;
try
{
m_pConnection->Open((_bstr_t)str.c_str(),"","",NULL);
}
catch(_com_error &e)
{
std::cout<<"打开失败:"<<e.Description()<<std::endl;
Sleep(-1);
return 0;
}
_RecordsetPtr recordsetP;
if(FAILED(recordsetP.CreateInstance(__uuidof( Recordset ))))
{
std::cout<<"新建Recordset对象失败:"<<std::endl;
Sleep(-1);
return 0;
}
try
{
//recordsetP->Open(_variant_t("Sell_Agent"),
// _variant_t((IDispatch *)m_pConnection,true), CursorTypeEnum::adOpenKeyset,LockTypeEnum::adLockOptimistic, CommandTypeEnum::adCmdTable);
//recordsetP->Open(_variant_t("Sell_Agent"),
// _variant_t((IDispatch *)m_pConnection,true), ::adOpenKeyset,::adLockOptimistic, ::adCmdTable);
}
catch (_com_error &e)
{
std::cout<<"打开失败"<<e.Description()<<std::endl;
}
//std::string cmdStr = "UPDATE Sell_Agent SET Integral = 21 WHERE id = 2";
std::string cmdStr = "select * from Sell_Agent WHERE id = 2";
_CommandPtr pCmdChange;
hr = pCmdChange.CreateInstance(__uuidof(Command));
if(!SUCCEEDED(hr))
{
std::cout<<"create Command object error"<<std::endl;
Sleep(-1);
return 0;
}
pCmdChange->ActiveConnection = m_pConnection;
pCmdChange->CommandText = (_bstr_t)cmdStr.c_str();
m_pConnection->Errors->Clear();
_variant_t recordsCount=0;
recordsetP = pCmdChange->Execute(&recordsCount, NULL, adCmdText);
std::cout<< (long)recordsCount<<std::endl;
try
{
std::cout<<recordsetP->GetFields()->Count<<std::endl;
recordsetP->MoveFirst();
while(!recordsetP->adoEOF)
{
int id_value =(int)recordsetP->Fields->Item[_variant_t("Id")]->Value;
std::string id = (_bstr_t)recordsetP->GetFields()->GetItem((long)0)->Name;
std::string name_value =(_bstr_t)recordsetP->GetFields()->GetItem((long)1)->Value;
std::string name = (_bstr_t)recordsetP->GetFields()->GetItem((long)1)->Name;
std::cout<<id.c_str()<<" :"<<id_value<<" "<<name.c_str()<<" :"<<name_value.c_str()<<endl;
recordsetP->MoveNext();
}
}
catch(_com_error &el)
{
std::cout<<el.Description()<<std::endl;
}
std::cout<<"完成"<<std::endl;
::CoUninitialize();
Sleep(-1);
return 0;
}
// adotest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#import "c:\Windows\System32\msado15.dll" no_namespace rename("EOF","adoEOF")
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
_ConnectionPtr m_pConnection;
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(!SUCCEEDED(hr))
{
std::cout<<"m_pConnection.CreateInstance(__uuidof(Connection)) error"<<std::endl;
Sleep(-1);
return 0;
}
std::string str = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Sell;Data Source=WUBIYU-PC";
m_pConnection->ConnectionString ="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Sell;Data Source=WUBIYU-PC" ;
try
{
m_pConnection->Open((_bstr_t)str.c_str(),"","",NULL);
}
catch(_com_error &e)
{
std::cout<<"打开失败:"<<e.Description()<<std::endl;
Sleep(-1);
return 0;
}
_RecordsetPtr recordsetP;
if(FAILED(recordsetP.CreateInstance(__uuidof( Recordset ))))
{
std::cout<<"新建Recordset对象失败:"<<std::endl;
Sleep(-1);
return 0;
}
try
{
//recordsetP->Open(_variant_t("Sell_Agent"),
// _variant_t((IDispatch *)m_pConnection,true), CursorTypeEnum::adOpenKeyset,LockTypeEnum::adLockOptimistic, CommandTypeEnum::adCmdTable);
//recordsetP->Open(_variant_t("Sell_Agent"),
// _variant_t((IDispatch *)m_pConnection,true), ::adOpenKeyset,::adLockOptimistic, ::adCmdTable);
}
catch (_com_error &e)
{
std::cout<<"打开失败"<<e.Description()<<std::endl;
}
//std::string cmdStr = "UPDATE Sell_Agent SET Integral = 21 WHERE id = 2";
std::string cmdStr = "select * from Sell_Agent WHERE id = 2";
_CommandPtr pCmdChange;
hr = pCmdChange.CreateInstance(__uuidof(Command));
if(!SUCCEEDED(hr))
{
std::cout<<"create Command object error"<<std::endl;
Sleep(-1);
return 0;
}
pCmdChange->ActiveConnection = m_pConnection;
pCmdChange->CommandText = (_bstr_t)cmdStr.c_str();
m_pConnection->Errors->Clear();
_variant_t recordsCount=0;
recordsetP = pCmdChange->Execute(&recordsCount, NULL, adCmdText);
std::cout<< (long)recordsCount<<std::endl;
try
{
std::cout<<recordsetP->GetFields()->Count<<std::endl;
recordsetP->MoveFirst();
while(!recordsetP->adoEOF)
{
int id_value =(int)recordsetP->Fields->Item[_variant_t("Id")]->Value;
std::string id = (_bstr_t)recordsetP->GetFields()->GetItem((long)0)->Name;
std::string name_value =(_bstr_t)recordsetP->GetFields()->GetItem((long)1)->Value;
std::string name = (_bstr_t)recordsetP->GetFields()->GetItem((long)1)->Name;
std::cout<<id.c_str()<<" :"<<id_value<<" "<<name.c_str()<<" :"<<name_value.c_str()<<endl;
recordsetP->MoveNext();
}
}
catch(_com_error &el)
{
std::cout<<el.Description()<<std::endl;
}
std::cout<<"完成"<<std::endl;
::CoUninitialize();
Sleep(-1);
return 0;
}