使用记录集对象添加记录:
BOOL CRentDVDDlg::RentDVDByRecordset(CString& strDVDID, CString& strRentName, CString& strRentDate)
{
BOOL bRet = FALSE;
_RecordsetPtr pRentRecordset;
pRentRecordset.CreateInstance(__uuidof(Recordset));
try {
HRESULT hr = pRentRecordset->Open(
_T("select * from tbRentInfo"),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hr))
{
pRentRecordset->AddNew();
pRentRecordset->PutCollect("sDVDID",_variant_t(strDVDID));
pRentRecordset->PutCollect("sName",_variant_t(strRentName));
pRentRecordset->PutCollect("sDate",_variant_t(strRentDate));
pRentRecordset->Update();
bRet = TRUE;
}
} catch(_com_error *e) {
MessageBox(e->ErrorMessage());
}
pRentRecordset->Close();
pRentRecordset = NULL;
return bRet;
}
原理是先构造一个记录集,把数据库下载到本地,然后通过pRentRecordset->AddNew();新建一条空的记录,使用PutCollect来设置值,最后用Update更新上传到数据库。
BOOL CRentDVDDlg::RentDVDByRecordset(CString& strDVDID, CString& strRentName, CString& strRentDate)
{
BOOL bRet = FALSE;
_RecordsetPtr pRentRecordset;
pRentRecordset.CreateInstance(__uuidof(Recordset));
try {
HRESULT hr = pRentRecordset->Open(
_T("select * from tbRentInfo"),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hr))
{
pRentRecordset->AddNew();
pRentRecordset->PutCollect("sDVDID",_variant_t(strDVDID));
pRentRecordset->PutCollect("sName",_variant_t(strRentName));
pRentRecordset->PutCollect("sDate",_variant_t(strRentDate));
pRentRecordset->Update();
bRet = TRUE;
}
} catch(_com_error *e) {
MessageBox(e->ErrorMessage());
}
pRentRecordset->Close();
pRentRecordset = NULL;
return bRet;
}
原理是先构造一个记录集,把数据库下载到本地,然后通过pRentRecordset->AddNew();新建一条空的记录,使用PutCollect来设置值,最后用Update更新上传到数据库。