ADO:实现向Oracle数据库中插入数据

ADO:实现向Oracle数据库中插入数据

1、使用Command对象完成插入。

Command 对象定义了将对数据源执行的指定命令。该对象中常用的属性和方法如下:
⑴ ActiveConnection 属性:该属性指定 Command 对象当前所属的 Connection 对象;
⑵ CommandText 属性:该属性包含发送给数据提供者的命令文本,该属性的值可以是 SQL 语句、表名 或 存储过程名 的字符串。
⑶ CommandType 属性:指示 Command 对象的属性,其值可以取 CommandTypeEnum 中的某个值,取值类似 Recordset 对象的 Open 方法的 Options 参数。
⑷ Execute 方法:执行在 CommandText 属性中指定的查询、SQL 语句 或 存储过程。

使用 Command 对象的过程步骤:
⑴ 定义 _CommandPtr 型变量;
⑵ 实例化变量;
⑶ 设置变量的相关属性;

⑷ 调用 Execute 方法执行命令。

实例代码:

[html] view plain copy

  1. /*
  2.     功能:使用 Command 对象向数据库中插入数据
  3. */
  4. BOOL CRentDVDDlg::RentDVDByCommand(CString& strDVDID, CString& strRentName, CString& strRentDate)
  5. {
  6.     BOOL bRet = FALSE;
  7.     HRESULT hr;
  8.     /*实例化一个Command对象*/
  9.     _CommandPtr pCommand;
  10.     hr = pCommand.CreateInstance(__uuidof(Command));
  11.     if(FAILED(hr))
  12.     {
  13.         MessageBox(_T("RentDVDByCommand:Command对象实例化失败!"));
  14.         return FALSE;
  15.     }
  16.     /*关联当前连接*/
  17.     pCommand->ActiveConnection = m_pConnection;
  18.     /*组串*/
  19.     CString strSQL;
  20.     strSQL.Format(_T("insert into tbRentInfo(sDVDID,sName,sDate) values(\'%s\',\'%s\',\'%s\')"), strDVDID, strRentName, strRentDate);
  21.     pCommand->CommandText =_bstr_t(strSQL);
  22.     /*执行插入操作*/
  23.     try
  24.     {
  25.         _variant_t vRecords;
  26.         hr = pCommand->Execute(&vRecords,NULL,adCmdText);
  27.         if (SUCCEEDED(hr))
  28.         {
  29.             bRet = TRUE;
  30.         }
  31.     }
  32.     catch(_com_error *e)
  33.     {
  34.         MessageBox(e->ErrorMessage());
  35.     }
  36.     return bRet;
  37. }

2、使用Connection对象完成插入数据的操作,与上一种比较类似:

[html] view plain copy

  1. /*
  2.     功能:使用 Connection 对象向数据库中插入数据
  3. */
  4. BOOL CRentDVDDlg::RentDVDByConnection(CString& strDVDID, CString& strRentName, CString& strRentDate)
  5. {
  6.     BOOL bRet = FALSE;
  7.     HRESULT hr;
  8.     /*组串*/
  9.     CString strSQL;
  10.     strSQL.Format(_T("insert into tbRentInfo(sDVDID,sName,sDate) values(\'%s\',\'%s\',\'%s\')"), strDVDID, strRentName, strRentDate);
  11.     /*执行插入操作*/
  12.     try
  13.     {
  14.         _variant_t vRecords,RecordsAffected;
  15.         hr = m_pConnection->Execute(_bstr_t(strSQL), &RecordsAffected, adCmdText);
  16.         if (SUCCEEDED(hr))
  17.         {
  18.             bRet = TRUE;
  19.         }
  20.     }
  21.     catch(_com_error *e)
  22.     {
  23.         MessageBox(e->ErrorMessage());
  24.     }
  25.     return bRet;
  26. }

3、使用RecordSet对象完成插入数据操作,RecordSet对象的使用前面文章有介绍:

[html] view plain copy

  1. /*
  2.     功能:使用 Connection 对象向数据库中插入数据
  3. */
  4. BOOL CRentDVDDlg::RentDVDByConnection(CString& strDVDID, CString& strRentName, CString& strRentDate)
  5. {
  6.     BOOL bRet = FALSE;
  7.     HRESULT hr;
  8.     /*组串*/
  9.     CString strSQL;
  10.     strSQL.Format(_T("insert into tbRentInfo(sDVDID,sName,sDate) values(\'%s\',\'%s\',\'%s\')"), strDVDID, strRentName, strRentDate);
  11.     /*执行插入操作*/
  12.     try
  13.     {
  14.         _variant_t vRecords,RecordsAffected;
  15.         hr = m_pConnection->Execute(_bstr_t(strSQL), &RecordsAffected, adCmdText);
  16.         if (SUCCEEDED(hr))
  17.         {
  18.             bRet = TRUE;
  19.         }
  20.     }
  21.     catch(_com_error *e)
  22.     {
  23.         MessageBox(e->ErrorMessage());
  24.     }
  25.     return bRet;
  26. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值