C + + Connect Oracle

Source: Internet
Author: User
Tags sprintf

Here is an ADO way to connect to Oracle's applet part of the code ...

First is the configuration of Oracle, located under the Oracle installation path: Oracle\network\admin\tnsnames.ora file, configure the connection configuration

[Plain]View plain copy
    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. )

Create a new header file, named CDBOperation.h:

[CPP]View plain copy
  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. //Initialize objects required for database operations
  7. Cdboperation (void);
  8. ~cdboperation (void);
  9. //Connect to database
  10. bool Conntodb (char *connectionstring, char *userid, char *password);
  11. //Database operation function
  12. //Query operation delete and add
  13. _RecordsetPtr executewithressql (const char *);
  14. Private
  15. void Printerrorinfo (_com_error &);
  16. Private
  17. //Initialize database connections, commands, recordset
  18. _connectionptr createconnptr ();
  19. _commandptr createcommptr ();
  20. _RecordsetPtr createrecsetptr ();
  21. Private
  22. //connection required for database connection, command action object
  23. _connectionptr m_pconnection;
  24. _commandptr M_pcommand;
  25. };

Create a new C + + source file named CDBOperation.cpp:

[CPP]View plain copy
  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::P rinterrorinfo (_com_error &e)
  51. {
  52. printf ("Error infomation is 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. }

My code is placed in the MFC one button click event:

Remember to import the header file in the CPP file that handles the event: #include "DBOperation.h"

[CPP]View plain copy
  1. Cdboperation Dboper;
  2. bool Bconn = Dboper.conntodb ("PROVIDER=ORAOLEDB.ORACLE.1;  Persist Security info=true;data Source=boss ", " username "," password ");
  3. if (false = = Bconn)
  4. {
  5. MessageBox ((lpctstr)"Connection Database Error", 0,0);
  6. return;
  7. }
  8. //Enquiry
  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 ("Query data Error!  "), 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 ();//record set pointer moved to the front of the query result set
  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 ("Insert data succeeded \ n"));
  38. }
  39. //Execute DELETE statement
  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 ("delete data successfully"), 0,0);
  45. }
  46. //Execute UPDATE statement
  47. sprintf (SQL, "Update boss_test_table2 set users_name = '%s '", " C + + anti-Human, MFC anti-social");
  48. pRst = dboper.executewithressql (sql);
  49. if (NULL! = pRst)
  50. {
  51. MessageBox (_t ("Update data successfully"), 0,0);
  52. }

C + + Connect Oracle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.