MFC数据库操作 - Command 对象删除、添加、修改记录

https://blog.csdn.net/a550853006a/article/details/45012749

使用 Command 对象的过程步骤:
⑴ 定义 _CommandPtr 型变量;
⑵ 实例化变量;
⑶ 设置变量的相关属性;
⑷ 调用 Execute 方法执行命令。

例一:通过 Command 对象 使用SQL语句来进行添加、删除、修改操作
BOOL bRet = FALSE;
_CommandPtr pCommand;
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = m_pConnection;

CString strSQL;
strSQL.Format(_T("insert into tbRentInfo(sDVDID,sName,sDate) values(\'%s\',\'%s\',\'%s\')"), strDVDID, strRentName, strRentDate);
pCommand->CommandText =_bstr_t(strSQL);

try {
HRESULT hr = pCommand->Execute(NULL, NULL, adCmdText);
if(SUCCEEDED(hr)){
bRet = TRUE;
}

}catch(_com_error *e) {
MessageBox(e->ErrorMessage());
}

例二:通过 _ConnectionPtr 数据库连接对象 使用SQL语句来进行添加、删除、修改操作
_bstr_t vSQL;
vSQL = _T("delete from tbRentInfo where ID=") + rentID;
_variant_t RecordsAffected;
try
{
HRESULT hr = m_pConnection->Execute(_bstr_t(vSQL), &RecordsAffected, adCmdText);
if(SUCCEEDED(hr))
{

}
}
catch (_com_error *e)
{
MessageBox(e->ErrorMessage());
return;
}
 

猜你喜欢

转载自blog.csdn.net/active2489595970/article/details/88550769
今日推荐