3. using namespace std; 4. 5. void main() 6. { 7. CDBOpe">

C++数据库操作

  • 格式:doc
  • 大小:68.50 KB
  • 文档页数:6

点击(此处)折叠或打开 1. #include "DBOperation.h"

2. #include 3. using namespace std; 4. 5. void main() 6. { 7. CDBOperation dbOper; 8. bool bConn = dbOper.ConnToDB("Provider=OraOLEDB.Oracle.1;Persist Security Info=True;Data Source=xxx1", "xxx2", "xxx3"); 9. if (false == bConn) 10. { 11. printf("连接数据库出现错误\n"); 12. system("PAUSE"); 13. return; 14. } 15. 16. _RecordsetPtr pRst; 17. 18. //执行查询语句 19. //char *sql = "select * from TSTUDENT"; 20. char sql[255] = {0}; 21. strcpy(sql, "select * from TSTUDENT"); 22. pRst = dbOper.ExecuteWithResSQL(sql); 23. if (NULL == pRst) 24. { 25. printf("查询数据出现错误!\n"); 26. system("PAUSE"); 27. return; 28. } 29. if (pRst->adoEOF) 30. { 31. pRst->Close(); 32. printf("There is no records in this table\n"); 33. return; 34. } 35. _variant_t vSno, vName, v***, vAge, vDno, vDname, vCname; 36. while (!pRst->adoEOF) 37. { 38. //pRst->MoveFirst(); //记录集指针移动到查询结果集的前面 39. vSno = pRst->GetCollect(_variant_t((long)0)); 40. vName = pRst->GetCollect(_variant_t("name")); 41. v*** = pRst->GetCollect(_variant_t("***")); 42. vAge = pRst->GetCollect(_variant_t("age")); 43. //vDno = pRst->GetCollect("dno"); 44. //vDname = pRst->GetCollect("dname"); 45. //vCname = pRst->GetCollect("cname"); 46. 47. printf("%s\t%s\t%s\t%d\n", (LPSTR)(LPCSTR)(_bstr_t)vSno, (LPSTR)(LPCSTR)_bstr_t(vName), (LPSTR)(LPCSTR)_bstr_t(v***), vAge.intVal); 48. pRst->MoveNext(); 49. } 50. 51. //执行插入语句 52. //sprintf(sql, "insert into TSTUDENT(sno, name, ***, age) values('%s', '%s', '%s', %d)", "20080016", "全局", "女", 25); 53. strcpy(sql, "insert into TSTUDENT(sno, name, ***, age) values('20080001', '全局', '女', 25)"); 54. pRst = dbOper.ExecuteWithResSQL(sql); 55. if (NULL != pRst) 56. { 57. printf("插入数据成功\n"); 58. } 59. //执行删除语句 60. 61. sprintf(sql, "delete from TSTUDENT where sno = '%s'", "20080017"); 62. pRst = dbOper.ExecuteWithResSQL(sql); 63. if (NULL != pRst) 64. { 65. printf("删除数据成功\n"); 66. } 67. system("PAUSE"); 68. //pRst->Close(); 69. }

其中XXX1:是tnsnames.ora中配置的服务名,XXX2是用户名,XXX3是密码。 DBOperation.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. //查询操作 删除以及添加 14. _RecordsetPtr ExecuteWithResSQL(const char *); 15. //bool ExecuteNoResSQL(const char *);//delete and add 16. 17. private: 18. void PrintErrorInfo(_com_error &); 19. 20. private: 21. //初始化数据库连接、命令、记录集 22. _ConnectionPtr CreateConnPtr(); 23. _CommandPtr CreateCommPtr(); 24. _RecordsetPtr CreateRecsetPtr(); 25. 26. private: 27. //数据库连接需要的连接、命令操作对象 28. _ConnectionPtr m_pConnection; 29. _CommandPtr m_pCommand; 30. };

DBOperation.cpp 点击(此处)折叠或打开 1. #include "DBOperation.h"

2. 3. CDBOperation::CDBOperation(void) 4. { 5. CoInitialize(NULL); 6. m_pConnection = CreateConnPtr(); 7. m_pCommand = CreateCommPtr(); 8. } 9. 10. CDBOperation::~CDBOperation(void) 11. { 12. //m_pCommand->Close(); 13. m_pConnection->Close(); 14. } 15. 16. bool CDBOperation::ConnToDB(char *ConnectionString, char *UserID, char *Password) 17. { 18. if (NULL == m_pConnection) 19. { 20. printf("Failed to create connection\n"); 21. return false; 22. } 23. 24. try 25. { 26. HRESULT hr = m_pConnection->Open(ConnectionString, UserID, Password, NULL); 27. if (TRUE == FAILED(hr)) 28. { 29. return false; 30. } 31. m_pCommand->ActiveConnection = m_pConnection; 32. return true; 33. } 34. catch(_com_error &e) 35. { 36. PrintErrorInfo(e); 37. return false; 38. } 39. } 40. 41. _RecordsetPtr CDBOperation::ExecuteWithResSQL(const char *sql) 42. { 43. //已经在连接至数据库的时候进行判断了 44. //if (NULL == m_pCommand || 0 == m_pConnection->State) 45. //{ 46. // printf("Failed to create command OR the state of connection is zero\n");