There are two main methods to develop the Oracle database interface in Visual C ++. One method is to use multiple database access technologies provided by Visual C ++, such as open database connection ODBC, well-known Data Access Object DAO, object connection and embedded database ole db and ActiveX Data Object ADO. Another method is to embed an SQL statement in Visual C ++, which refers to Pro * C/C ++ (PROC in this article ). The previous method is convenient and portable when you are familiar with VC programming because of the powerful class library support of MFC. However, compared with PROC, applications need to go through two layers to establish connections with database communication interfaces. The programming is relatively complex and the execution efficiency is relatively low. PROC supports embedded PL/SQL
Directly calling the Oracle database, such as block, combines Procedural language with non-procedural language to form a stronger development tool, which can develop optimized applications that meet various complex requirements, with high execution efficiency. Suitable for Oracle technical personnel. However, applications developed using PROC cannot be transplanted to heterogeneous database platforms.
This article describes in detail how to use PROC to develop an Oracle database interface program in the Visual C ++ environment and provides a programming example. This article takes Visual C ++ 6.0 and Oracle8i as examples. Other versions can be changed based on actual conditions.
Several special files
When PROC develops the Oracle database interface in VC, several special files are required.
1. the executable PROCUI. EXE of PROC
Use Oracle_HOME to represent the root directory of Oracle after installation. When it is installed on the computer's D disk by default, Oracle_HOME is located at D:/Oracle. In this case, the executable file of PROC is Oracle_HOME/Ora81/BIN/PROCUI. EXE, which is installed on D:/Oracle/Ora81/BIN/PROCUI. EXE by default.
2. Oracle supports the database file OraSQL8. LIB of SQL in the VC Environment
According to the preceding conventions, the OraSQL8. LIB file is installed on Oracle_HOME/Ora81/PRECOMP/LIB/MSVC/OraSQL8.LIB. The default installation is on D:/Oracle/Ora81/PRECOMP/LIB/MSVC/OraSQL8.LIB.
3. Oracle supports SQL header files in the VC Environment
According to the above conventions, the header file *. h in Oracle_HOME/Ora81/PRECOMP/PUBLIC /*. h. The default installation is in D:/Oracle/Ora81/PRECOMP/PUBLIC /*. h.
*. H is a general term for header files. There are usually more than 10 header files. The specific content can be found in the specified path.
Integrate PROC into VC Environment
To facilitate the use of PROC to develop the Oracle database interface in VC, the PROC is usually integrated into the Visual C ++ 6.0 environment, directly use the PROC pre-compiler in the C/C ++ environment to pre-compile the application, and then compile and link it to generate an executable program. Integrate PROC into the VC environment to complete the following tasks.
1. Add PROC to the Tools menu list
A) run Microsoft Visual C ++ 6.0;
B) Select the mimize item from the menu item Tools. For the sake of simplicity, it is written in the following format: menu Tools/Customize item. Similar expressions are used below. The Customize dialog box is displayed;
C) Click the Tools tab (or property page) and move the "Menu contents" box to the bottom area with the mouse;
D) double-click the dotted line rectangle area, enter "PROC" in the blank area, and press Enter;
E) In the "Command" box, enter the executable file name of PROC. According to the description in section 2.1, enter D:/Oracle/Ora81/BIN/PROCUI. EXE for the default installation;
F) In the "Arguments" box, enter "$ (TargetName )". When the PROC item is selected from the menu Tools, VC will pass the current project name to PROC, and then PROC will directly open the file with the same name extension. pre under the project file directory;
G) In the "Initial directory" box, enter "$ (WkspDir)"/click "Close" to complete PROC integration into the VC environment.
2. Specify the header file path
To ensure that the VC compilation link is completed smoothly, you need to add the header file provided by Oracle to the VC environment. Follow these steps to specify the path of the header file.
A) menu Tools/Options items. The "Options" dialog box appears;
B) Click the "Directories" tab and select "Include files" from the "Show directories for:" list box ";
C) Move the scroll bar of the Directories box to the bottom area;
D) double-click the dot-line rectangular area and enter a subdirectory that contains the SQL header file supported by Oracle in the VC environment in the blank area. According to section 2.3, enter D for the default installation: /Oracle/Ora81/PRECOMP/PUBLIC.
Process of developing an Oracle interface program under VC
1. Create a new project
In the following description, assume that the new project is named Exam01. After running Visual C ++ 6.0, follow these steps:
A) menu File/New item/Project card;
B) Select the Win32 console Application item;
C) select by browsing or directly enter the project path/enter the created Project name, for example, Exam01;
D) Click the OK button, click Finish by default, and click OK to create the console application engineering framework.
2. Create a pre-compiled source file
Assume that the created pre-compiled source file is named Exam01.pc. Follow these steps in the Visual C ++ 6.0 environment:
A) menu Project/Add To Project item/New item;
B) Files card/SQL script File item;
C) Enter Exam01.pc in the Files editing box/click OK;
D) enter the Exam01.pc source file in the editing status, or copy the file from other files and then modify the file to form the Exam01. pc source file;
E) select an appropriate path to save the source file. For example, the path is E:/PROCW/Exam01.
3. Pre-compile
Through pre-compilation, the pre-compiled source file such as Exam01.pc is converted into the C program source file of Exam01.c. The procedure for integrating PROC into a VC environment is as follows:
A) menu Tools/PROC items;
B) Click OK when a dialog box without Exam01.pre is displayed. The PROC pre-compilation dialog box is displayed;
C) Add the pre-compiled source files such as Exam01.pc and its path to the Input items in the pre-compiled dialog box by adding items in the menu or clicking the "+" button, e:/PROCW/Exam01/Exam01.pc appears in the Input item. In this case, the Output file such as Exam01.c and path are automatically displayed in the Output item (the file name and path can be modified if necessary), that is, E:/PROCW/Exam01/Exam01.c appears in the Output item;
D) If necessary, double-click the Options option in the pre-compilation dialog box. In the displayed Options dialog box, select the required pre-compilation option (this step is generally not required, use the default pre-compilation option );
E) Click the pre-compilation icon on the rightmost of the toolbar to perform pre-compilation;
F) The pre-compilation is complete. If you are asked to save the Exam01.pre file, select OK to save the file and complete the pre-compilation;
G) if the pre-compilation is complete, when the status icon on the left of the pre-compilation dialog box is yellow (warning) or red (pre-compilation failed), double-click the icon to observe the help or error information. If the Pre-compilation fails, you should redo the editing work in section 4.2, modify the source program, and then pre-compile until the pre-compilation is passed.
4. Compilation preparation
To enable the project to be compiled, you need to add the pre-compiled project source files and the running database files that Oracle supports SQL in the VC environment to the project, the following describes how to add these two files.
1) Add pre-compiled output files to the Project
A) menu Project/Add To Project item/Files item;
B) In the file dialog box, select the correct path (see section 4.2 and section 4.3), select the pre-compiled output file, for example, Exam01.c, and click the OPEN button, add pre-compiled and output project source files to the project.
2) Add the running library file to the Project
A) menu Project/Add To Project item/Files item;
B) Change the file type of the file dialog box to "all files ";
C) Select Oracle_HOME/Ora81/PRECOMP/LIB/MSVC as the path;
D) Select the OraSQL8.LIB file and click the OPEN button to add the running library file to the project.
5. Compile the link
A) Press F7 or click the compilation icon to compile the project. If no error occurs, compile the link and generate an executable file, for example, exam01.exe;
B) if an error occurs in the compilation link, return to section 4.2 and select the corresponding pre-compilation source file, such as Exam01.pc, to modify and save it. Then, follow section 4.3 for pre-compilation. After the pre-compilation is complete, click OK to use the new token.
6. Run the project
A) Press ctrl_f5or single-click to execute the graphic operation project exam01.exe. Follow the prompts in the project to gradually run correctly;
B) if an error occurs during running, return to section 4.2 to modify the corresponding pre-compiled source file, perform pre-compilation in section 4.3, compile the link in section 4.5, and generate a new executable file, then run the project again until the specified tasks of the project are correctly implemented.
Programming example
1. program content
Generally, SQL embedded programs are mainly composed of instructions, including header files, subroutine declarations, main programs, and subprograms. Related subprograms are called in the main program. Necessary subprograms include database subprograms, database disconnection subprograms, error processing subprograms, and working subprograms that complete a specific transaction (such as query, insertion, modification, and deletion.
2. program example
The following is a complete example program that can be run through pre-compilation, compilation links.
/* Exam01.example of developing an Oracle interface program for PC */
/* Description: This program describes the programming features of using PROC to develop the Oracle database interface. To AUTHS
* Enter the writer code in the table to query the writer's name and salary. Create a table, insert data, and submit the table before running. */
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
/* Contains the SQL Communication zone, which is used to handle errors. */
# Include <sqlca. h>
Void connect ();/* connect to Oracle Server */
Void disconnect ();/* disconnect from Oracle Server */
Void SQL _error (char *);/* handle error handle */
Void select ();/* query subroutine */
Extern sqlglm (char *, int *, int *);
/* Main Program */
Void main ()
{
/* Handle installation errors */
Exec SQL whenever sqlerror do SQL _error ("Oracle error --/n ");
/* Connect to the database */
Connect ();
/* Execute the query */
Select ();
/* Disconnect the database */
Disconnect ();
}
/* Subroutine */
/* Connect subprogram connect ()*/
Void connect ()
{
Exec SQL BEGIN DECLARE SECTION;
VARCHAR username [10], password [10], server [10];
Exec SQL END DECLARE SECTION;
/* Enter the user name, password, and server name */
Printf ("/n input Username :");
Gets (username. arr );
Username. len = (unsigned short) strlen (char *) username. arr );
Printf ("/n input password :");
Gets (password. arr );
Password. len = (unsigned short) strlen (char *) password. arr );
Printf ("/n input server name :");
Gets (server. arr );
Server. len = (unsigned short) strlen (char *) server. arr );
/* Connect to the Oracle server */
Exec SQL CONNECT: username IDENTIFIED BY: password USING: server;
Printf ("/n successfully connected to the server % s as USER % s! /N ", username. arr, server. arr );
}
/* Disconnect the connection subroutine disconnect ()*/
Void disconnect ()
{
Char temp;
Printf ("/n: Do you want to submit all transactions before disconnecting? (Y/N )");
Scanf ("% c", & temp );
Fflush (stdin );
If (temp! = 'Y' & temp! = 'Y ')
{
/* Roll back the transaction and disconnect. */
Exec SQL ROLLBACK WORK RELEASE;
Printf ("/n roll back the transaction, disconnect, and exit the program! /N ");
}
Else
{
/* Submit the transaction and disconnect. */
Exec SQL COMMIT WORK RELEASE;
Printf ("/n commit transaction, disconnect and exit the program! /N ");
Exit (1 );
}
}
/* Query subprogram select ()
* First, enter the writer code and then query the writer's name and salary. */
Void select ()
{
Exec SQL BEGIN DECLARE SECTION;
Char author_code [8], name [10];
Float salary;
Short salary_ind;
Exec SQL END DECLARE SECTION;
Printf ("/n input writer Code :");
Gets (author_code );
/* Query writer's name and salary */
Exec SQL SELECT name, salary INTO: name,: salary: salary_ind
FROM auths
WHERE author_code =: author_code;
/* Determine whether the writer's salary is empty based on the value of the directive variable. */
If (salary_ind = 0)
{
Printf ("/n writer code/t writer name/t writer salary/n ");
Printf ("--------/t --------/n ");
Printf ("% 8 s/t % 8 s/t % 8.2f/n", author_code, name, salary );
}
Else
{
Printf ("writer % s's salary is not input, it is null! /N ", name );
}
}
/* Error handling subroutine SQL _error ()*/
Void SQL _error (char * msg)
{
Char err_msg [128];
Size_t buf_len, msg_len;
/* If an SQL error occurs, continue to execute. */
Exec SQL WHENEVER SQLERROR CONTINUE;
Printf ("/n % s/n", msg );
Buf_len = sizeof (err_msg );
/* Call the sqlglm () function to obtain the error message. */
Sqlglm (err_msg, & buf_len, & msg_len );
Printf ("%. * s/n", msg_len, err_msg );
/* Roll back the transaction, disconnect, and exit the program. */
Exec SQL ROLLBACK RELEASE;
Exit (EXIT_FAILURE );
}
3. Create tables and insert data records
To run the preceding example program correctly, you must log on to the Oracle database as a valid user, create an AUTHS table, and insert some data records. The example commands for creating a table file, creating a table, and inserting data records are as follows. After the work described here is complete, the executable files generated in the previous section can run correctly.
REM is the table creation file auths. SQL
Drop table auths CASCADE CONSTRAINTS
/
Create table auths (
AUTHOR_CODE VARCHAR2 (8) not null,
NAME VARCHAR2 (10 ),
Birthdate date,
ENTRY_DATE_TIME DATE,
Salary number (7, 2 ),
Remark VARCHAR2 (255 ))
/
The next line of REM is the command to run the table creation file in the PL/SQL environment.
REM @ E:/PROCW/Exam01/auths. SQL
The next line of REM is the command to insert data to the auths table in the PL/SQL environment. After insertion, it should be submitted (COMMIT )!
Rem insert into auths (author_code, name, salary) VALUES ('a0000001', 'wang Dalin ', 1200 );