C++ 连接Oracle

时间:2023-03-09 22:41:10
C++ 连接Oracle

下面是一个ADO方式连接Oracle的小程序部分代码......

首先是Oracle的配置、在Oracle的安装路径下找到:Oracle\network\ADMIN\tnsnames.ora文件、配置一下连接配置

  1. BOSS =
  2. (DESCRIPTION =
  3. (ADDRESS_LIST =
  4. (ADDRESS = (PROTOCOL = TCP)(HOST = xx.xx.xx.xx)(PORT = 1521))
  5. )
  6. (CONNECT_DATA =
  7. (SERVICE_NAME = boss)
  8. )
  9. )

新建一个头文件、名为CDBOperation.h:

  1. #pragma once
  2. #import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
  3. class CDBOperation
  4. {
  5. public:
  6. //初始化数据库操作需要的对象
  7. CDBOperation(void);
  8. ~CDBOperation(void);
  9. //连接至数据库
  10. bool ConnToDB(char *ConnectionString, char *UserID, char *Password);
  11. //数据库操作函数
  12. //查询操作 删除以及添加
  13. _RecordsetPtr ExecuteWithResSQL(const char *);
  14. private:
  15. void PrintErrorInfo(_com_error &);
  16. private:
  17. //初始化数据库连接、命令、记录集
  18. _ConnectionPtr CreateConnPtr();
  19. _CommandPtr CreateCommPtr();
  20. _RecordsetPtr CreateRecsetPtr();
  21. private:
  22. //数据库连接需要的连接、命令操作对象
  23. _ConnectionPtr m_pConnection;
  24. _CommandPtr m_pCommand;
  25. };

新建一个c++源文件、名为CDBOperation.cpp:

  1. #include "stdafx.h"
  2. #include "DBOperation.h"
  3. CDBOperation::CDBOperation(void)
  4. {
  5. CoInitialize(NULL);
  6. m_pConnection = CreateConnPtr();
  7. m_pCommand = CreateCommPtr();
  8. }
  9. CDBOperation::~CDBOperation(void)
  10. {
  11. m_pConnection->Close();
  12. }
  13. bool CDBOperation::ConnToDB(char *ConnectionString, char *UserID, char *Password)
  14. {
  15. if (NULL == m_pConnection)
  16. {
  17. printf("Failed to create connection\n");
  18. return false;
  19. }
  20. try
  21. {
  22. HRESULT hr = m_pConnection->Open(ConnectionString, UserID, Password, NULL);
  23. if (TRUE == FAILED(hr))
  24. {
  25. return false;
  26. }
  27. m_pCommand->ActiveConnection = m_pConnection;
  28. return true;
  29. }
  30. catch(_com_error &e)
  31. {
  32. PrintErrorInfo(e);
  33. return false;
  34. }
  35. }
  36. _RecordsetPtr CDBOperation::ExecuteWithResSQL(const char *sql)
  37. {
  38. try
  39. {
  40. m_pCommand->CommandText = _bstr_t(sql);
  41. _RecordsetPtr pRst = m_pCommand->Execute(NULL, NULL, adCmdText);
  42. return pRst;
  43. }
  44. catch(_com_error &e)
  45. {
  46. PrintErrorInfo(e);
  47. return NULL;
  48. }
  49. }
  50. void CDBOperation::PrintErrorInfo(_com_error &e)
  51. {
  52. printf("Error infomation are as follows\n");
  53. printf("ErrorNo: %d\nError Message:%s\nError Source:%s\nError Description:%s\n", e.Error(), e.ErrorMessage(), (LPCTSTR)e.Source(), (LPCTSTR)e.Description());
  54. }
  55. _ConnectionPtr CDBOperation::CreateConnPtr()
  56. {
  57. HRESULT hr;
  58. _ConnectionPtr connPtr;
  59. hr = connPtr.CreateInstance(__uuidof(Connection));
  60. if (FAILED(hr) == TRUE)
  61. {
  62. return NULL;
  63. }
  64. return connPtr;
  65. }
  66. _CommandPtr CDBOperation::CreateCommPtr()
  67. {
  68. HRESULT hr;
  69. _CommandPtr commPtr;
  70. hr = commPtr.CreateInstance(__uuidof(Command));
  71. if (FAILED(hr) == TRUE)
  72. {
  73. return NULL;
  74. }
  75. return commPtr;
  76. }
  77. _RecordsetPtr CDBOperation::CreateRecsetPtr()
  78. {
  79. HRESULT hr;
  80. _RecordsetPtr recsetPtr;
  81. hr = recsetPtr.CreateInstance(__uuidof(Command));
  82. if (FAILED(hr) ==TRUE)
  83. {
  84. return NULL;
  85. }
  86. return recsetPtr;
  87. }

我的代码是放在MFC一个按钮Click事件里面的:

记住在处理事件的cpp文件中导入头文件:#include "DBOperation.h"

  1. CDBOperation dbOper;
  2. bool bConn = dbOper.ConnToDB("Provider=OraOLEDB.Oracle.1;Persist Security Info=True;Data Source=boss", "用户名", "密码");
  3. if (false == bConn)
  4. {
  5. MessageBox((LPCTSTR)"连接数据库出现错误\0",0,0);
  6. return;
  7. }
  8. //查询
  9. _RecordsetPtr pRst;
  10. char sql[255] = {0};
  11. strcpy(sql, " select * from boss_test_table2 where rownum = 1 ");
  12. pRst = dbOper.ExecuteWithResSQL(sql);
  13. if (NULL == pRst)
  14. {
  15. MessageBox(_T("查询数据出现错误!\0"),0,0);
  16. return;
  17. }
  18. if (pRst->adoEOF)
  19. {
  20. pRst->Close();
  21. MessageBox((LPCTSTR)"There is no records in this table\0",0,0);
  22. return;
  23. }
  24. _variant_t vSno, vName;
  25. while (!pRst->adoEOF)
  26. {
  27. //pRst->MoveFirst(); //记录集指针移动到查询结果集的前面
  28. vSno = pRst->GetCollect(_variant_t("U_NUMBER"));
  29. vName = pRst->GetCollect(_variant_t("USERS_NAME"));
  30. MessageBox((LPCTSTR)(_bstr_t)vSno,0,0);
  31. pRst->MoveNext();
  32. }
  33. strcpy(sql, "insert into boss_test_table2 (u_number, users_name, users_phone, status, customno_id) values ('0001', 'C+TTT+', '13999000000', 2, 'BPPPPPPPPPP')");
  34. pRst = dbOper.ExecuteWithResSQL(sql);
  35. if (NULL != pRst)
  36. {
  37. AfxMessageBox(_T("插入数据成功\n"));
  38. }
  39. //执行删除语句
  40. sprintf(sql, "delete boss_test_table2 where u_number = '%s'", "009");
  41. pRst = dbOper.ExecuteWithResSQL(sql);
  42. if (NULL != pRst)
  43. {
  44. MessageBox(_T("删除数据成功\0"),0,0);
  45. }
  46. //执行更新语句
  47. sprintf(sql, "update boss_test_table2 set users_name = '%s' ", "C++反人类、MFC反社会");
  48. pRst = dbOper.ExecuteWithResSQL(sql);
  49. if (NULL != pRst)
  50. {
  51. MessageBox(_T("更新数据成功\0"),0,0);
  52. }