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

简介: ADO:实现向Oracle数据库中插入数据 1、使用Command对象完成插入。 Command 对象定义了将对数据源执行的指定命令。该对象中常用的属性和方法如下: ⑴ ActiveConnection 属性:该属性指定 Command 对象当前所属的 Connection 对象; ⑵ CommandText 属性:该属性包含发送给数据提供者的命令文本,该属性的值可以是 SQL 语句、表名 或 存储过程名 的字符串。

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. }
相关文章
|
2天前
|
Oracle 安全 关系型数据库
|
1天前
|
Oracle 关系型数据库 数据库
|
2天前
|
存储 Oracle 关系型数据库
|
1天前
|
SQL Oracle 关系型数据库
关系型数据库Oracle设置 RMAN 环境:
【7月更文挑战第25天】
8 2
|
3天前
|
存储 Oracle 关系型数据库
关系型数据库Oracle运行RMAN脚本
【7月更文挑战第23天】
11 4
|
1天前
|
SQL Oracle 关系型数据库
关系型数据库Oracle结束 RMAN 会话:
【7月更文挑战第25天】
6 1
|
3天前
|
监控 Oracle 算法
|
5天前
|
存储 监控 Oracle
关系型数据库Oracle备份策略建议
【7月更文挑战第21天】
17 6
|
5天前
|
存储 Oracle 关系型数据库
|
6天前
|
监控 Oracle 关系型数据库
关系型数据库Oracle恢复测试
【7月更文挑战第20天】
18 7

推荐镜像

更多