Oracle Database Programming Application Instance description

Source: Internet
Author: User

The following articles mainly describe the Oracle Database Programming Application Instance using ADO in Visual C ++. If you are interested in it, you can click the following articles to view it. If you are curious about this newly developed technology, the following articles will unveil its mysteries.

1. Generate the application framework and initialize the OLE/COM library Environment

Create a Standard MFC AppWizard (exe) application, and then initialize the OLE/COM library in the InitInstance function of the ADO database because the ADO library is a com dll library ).
In this example:

 
 
  1. BOOL CAdotestDlg::OnInitDialog()  
  2. {  
  3. ::CoInitialize(NULL);  

Initialize the OLE/COM library Environment
}

The program must call: CoUninitialize (); // release the COM resources occupied by the program.

In addition:

M_pRecordset->Close (); note !!! Do not close it multiple times !!!!!!!!!!!!

M_pConnection->Close ();

M_pRecordset = NULL;

M_pConnection = NULL;
 

2. Introduce the ADO Library File

Before using ADO, you must use the import symbol # import to introduce the ADO library file at the end of the stdafx. h file of the project so that the compiler can compile it correctly. The Code is as follows:

 
 
  1. #import "C:\Program Files\common files\system\ado\msado15.
    dll" no_namespace rename("EOF","adoEOF") 

ADO class is defined as a kind of resource stored in ado dll (msado15.dll), which is internally called a Type Library. The Type Library describes the autonomous interface and the COM vtable interface used by C ++. When using the # import command, Visual C ++ needs to read this type of library from ado dll at runtime and create a set of C ++ header files.

These header files have the. tli and. tlh extensions, which can be found in the project directory. The ADO class called in the C ++ program code should be defined in these files. The third line of the program indicates that the ADO object does not use namespaces. In some applications, namespace is necessary because the objects in the application may conflict with those in ADO.

If you want to use a namespace, you can change the third-line program to rename_namespace ("AdoNS "). The fourth line of code changes the EOF In ADO to adoEOF to avoid conflicts with other libraries that define their own EOF.

3. Use smart pointers for Oracle Database Operations

Define two ADO smart pointer instances in the CaboutDlg header file, and add a ListCtrl in the dialog box.
 
 

 
 
  1. class CAdotestDlg : public CDialog  
  2. {  
  3. _ConnectionPtr m_pConnection;  
  4. _RecordsetPtr m_pRecordset;  
  5.  ClistCtrl m_List;   
  6. ......  
  7. }  

The ADO library contains three smart pointers: _ ConnectionPtr, _ CommandPtr, and _ RecordsetPtr.

_ ConnectionPtr is usually used to create a data connection or execute an SQL statement that does not return any results, such as a stored procedure.

_ CommandPtr returns a record set. It provides a simple method to execute stored procedures and SQL statements that return record sets. When using the _ CommandPtr interface, you can use the global _ ConnectionPtr interface, or

The _ CommandPtr interface directly uses the connection string. _ RecordsetPtr is a record set object. Compared with the above two types of objects, it provides more control functions for the record set, such as record lock and cursor control. In the event response using the ADO program, add the following code to OnButton1:

 
 
  1. void CAdotestDlg::OnButton1()   
  2. {  
  3. m_List.ResetContent();  
  4. m_pConnection.CreateInstance(_uuidof(Connection));  

Initialize Connection pointer

 
 
  1. m_pRecordset.CreateInstance(_uuidof(Recordset)); 

Initialize Recordset pointer

 
 
  1. try  
  2. {  
  3. m_pConnection->Open("DSN=ADOTest","","",0);  

Connect the ODBC data source called ADOTest. Note: This is an open function that does not require a user ID or password to connect. Otherwise, the format is->

 
 
  1. Open("DSN=test;uid=sa;pwd=123;","","",0);  

Execute the SQL statement to obtain a record set and assign its pointer to m_pRecordset.

 
 
  1. CString strSql="select * from middle";  
  2. BSTR bstrSQL = strSql.AllocSysString();   
  3. m_pRecordset->Open(bstrSQL,(IDispatch*)m_pConnection,
    adOpenDynamic,adLockOptimistic,adCmdText);  

AdOpenDynamic: Dynamic adLockOptimistic optimistic blocking method adshorttext: Text query statement

 
 
  1. while(!m_pRecordset->adoEOF) 

Traverse all records. The above content is a description of the specific operations of an instance programmed in the Oracle database using ADO in Visual C ++, I hope it will help you in this regard.

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.