#include <iostream>
#include <windows.h>
#include <atlstr.h>
#import "c:Program FilesCommon FilesSystemadomsado15.dll" no_namespace rename("EOF"??"adoEOF") rename("BOF"??"adoBOF")
class ADOConn
{
// ???????
public:
//?????????Connection????????:
_ConnectionPtr m_pConnection;
//?????????Recordset????????:
_RecordsetPtr m_pRecordset;
// ???巽??
public:
ADOConn()
{
}
virtual ~ADOConn()
{
}
// ?????—?????????
void  OnInitADOConn()
{
// ?????OLE/COM????
::CoInitialize(NULL);
HRESULT hr;
try
{
// ????Connection????
hr = m_pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
m_pConnection->ConnectionTimeout=600;//?????????????
m_pConnection->CommandTimeout=120;//???????????????
//DSN???????????
m_pConnection->Open("DSN=test; Server=localhost; Database=test"?? "root"?? "123456"?? adModeUnknown);
}
}
// ?????
catch(_com_error e)
{
// ??????????
std::cerr << "open error" << e.Description() << std::endl;
//AfxMessageBox(e.Description());
}
}
// ??в??
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL)
{
try
{
// ????????????Connection???????????????????????
if(m_pConnection==NULL)
OnInitADOConn();
// ?????????????
m_pRecordset.CreateInstance(__uuidof(Recordset));
// ?????е???
/*
Open ?????????????????:HRESULT Recordset15::Open ( const _variant_t & Source?? const _variant_t & ActiveConnection??
enum CursorTypeEnum CursorType?? enum LockTypeEnum LockType?? long Options )
??Source ?????????????
??ActiveConnection??????????????????????????Connection???????????????? _variant_t????)
??CursorType??????????????????????????????????:
enum CursorTypeEnum
{
adOpenUnspecified = -1??///??????????
adOpenForwardOnly = 0??///?????????????????????????????????????MoveNext??????????????????????????????????? BookMark??RecordCount??AbsolutePosition??AbsolutePage?????????
adOpenKeyset = 1??///????????????????????????????????????????????????????????м??????????????????
adOpenDynamic = 2??///????????????????????????????????????????????????
adOpenStatic = 3///????????????????????????????????????????????????????????2????????????????????????
};
??LockType?????? ????????????????????????????????
enum LockTypeEnum
{
adLockUnspecified = -1??///δ???
adLockReadOnly = 1??///????????
adLockPessimistic = 2????????????????????????????????????ж?????????????????????
adLockOptimistic = 3????????????????????????Update???????????????????????????????????????????????????
adLockBatchOptimistic = 4?????????????????????????????????????????????????????????ɡ?
};
*/
m_pRecordset->Open(bstrSQL?? m_pConnection.GetInterfacePtr()?? adOpenDynamic?? adLockOptimistic?? adCmdText);
}
// ?????
catch(_com_error e)
{
// ??????????
std::cerr << "get error" << e.Description() << std::endl;
//AfxMessageBox(e.Description());
}
// ????????
return m_pRecordset;
}
// ???SQL???Insert Update _variant_t
BOOL ExecuteSQL(_bstr_t bstrSQL)
{
try
{
// ???????????????
if(m_pConnection == NULL)
OnInitADOConn();
// Connection?????Execute????:(_bstr_t CommandText?? VARIANT * RecordsAffected?? long Options )
// ????CommandText????????????????SQL???
// ????RecordsAffected?????????????????????
// ????Options???CommandText???????adCmdText-???????adCmdTable-???? adCmdProc-?洢?????adCmdUnknown-δ?
m_pConnection->Execute(bstrSQL?? NULL?? adCmdText);
return true;
}
catch(_com_error e)
{
std::cerr << "execute error" << e.Description() << std::endl;
//AfxMessageBox(e.Description());
return false;
}
}
void ExitConnect()
{
// ?????????????
if (m_pRecordset != NULL)
m_pRecordset->Close();
if(m_pConnection != NULL)
m_pConnection->Close();
// ??????
::CoUninitialize();
}
};
int main()
{
int i;
ADOConn m_ADO;
m_ADO.OnInitADOConn();
//????SELECT???
_bstr_t vSQL;
vSQL = "SELECT * FROM test where id = 2";
//???SELETE???
_RecordsetPtr m_pRecordset;
m_pRecordset = m_ADO.GetRecordSet(vSQL);
CString name0;
//??????е??
while (!m_pRecordset->adoEOF)
{
name0 = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("name");
printf("name : %s "?? (CT2A)name0);
std::cerr << "success " << (CT2A)name0 << std::endl;
m_pRecordset->MoveNext();
}
//????INSERT???
/*
_bstr_t vInserSQL;
vInserSQL = "insert into test values(3?? 'cc')";
//???INSERT???
if (m_ADO.ExecuteSQL(vInserSQL))
{
printf("Insert Data Successful!!! ");
}
*/
/*
//????UPDATE???
_bstr_t vUpdateSQL;
vUpdateSQL = "update test set name = '????' where id = 3";
//???INSERT???
if (m_ADO.ExecuteSQL(vUpdateSQL))
{
printf("Update Data Successful!!! ");
}
*/
/*
//????UPDATE???
_bstr_t vDeleteSQL;
vDeleteSQL = "delete from test where id = 3";
//???INSERT???
if (m_ADO.ExecuteSQL(vDeleteSQL))
{
printf("Delete Data Successful!!! ");
}
*/
//???????????????
m_ADO.ExitConnect();
std::cin >> i;
return 0;
}