zoukankan      html  css  js  c++  java
  • C++ 连接Oracle

    刚刚学习了C++、感觉学东西还是动手比较学得快一点!

    下面是一个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.     //连接至数据库  
    11.     bool ConnToDB(char *ConnectionString, char *UserID, char *Password);  
    12.   
    13.     //数据库操作函数  
    14.     //查询操作 删除以及添加  
    15.     _RecordsetPtr ExecuteWithResSQL(const char *);  
    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. };  

    新建一个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.   
    56. _ConnectionPtr CDBOperation::CreateConnPtr()  
    57. {  
    58.     HRESULT hr;  
    59.     _ConnectionPtr connPtr;  
    60.     hr = connPtr.CreateInstance(__uuidof(Connection));  
    61.     if (FAILED(hr) == TRUE)  
    62.     {  
    63.         return NULL;  
    64.     }  
    65.     return connPtr;  
    66. }  
    67.   
    68. _CommandPtr CDBOperation::CreateCommPtr()  
    69. {  
    70.     HRESULT hr;  
    71.     _CommandPtr commPtr;  
    72.     hr = commPtr.CreateInstance(__uuidof(Command));  
    73.     if (FAILED(hr) == TRUE)  
    74.     {  
    75.         return NULL;  
    76.     }  
    77.     return commPtr;  
    78. }  
    79.   
    80. _RecordsetPtr CDBOperation::CreateRecsetPtr()  
    81. {  
    82.     HRESULT hr;  
    83.     _RecordsetPtr recsetPtr;  
    84.     hr = recsetPtr.CreateInstance(__uuidof(Command));  
    85.     if (FAILED(hr) ==TRUE)  
    86.     {  
    87.         return NULL;  
    88.     }  
    89.     return recsetPtr;  
    90. }  

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


    哎呀、又有工单了.......一个JS浏览器的兼容问题、有得搞了......o(︶︿︶)o 唉!!!

  • 相关阅读:
    c#自动更新+安装程序的制作
    VS2013项目受源代码管理向源代码管理注册此项目时出错
    WinDbg配置和使用基础
    InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
    PowerDesigner 如何生成数据库更新脚本
    用户故事(User Story)
    Troubleshooting Record and Playback issues in Coded UI Test
    Coded UI
    compare two oracle database schemas
    How to: Use Schema Compare to Compare Different Database Definitions
  • 原文地址:https://www.cnblogs.com/wwwroot/p/2729584.html
Copyright © 2011-2022 走看看