[ZT] Developing an Oracle Database Method with VC

xiaoxiao2021-03-06  14

Developed with VC based on Oracle database method is taken from: IT365

Author: Li Jie Mai Van

1 Introduction

Oracle has launched a SQL standard relational database product from 1979 to the 1997 version 8's launch, Oracle databases support high-performance transaction processing of large databases, multi-users, support, complete security and Integrity control, support distributed database and distribution processing, portability, compatibility, and connectivity, etc., have a favorite of user favorite, according to the market report of the World UNIX database according to IDG1992, Oracle accounts for 50% of the market sales. . In terms of development tools in the client, Visual C is also popular among the majority of programmers due to its strong function and high flexibility, so this article is intended to introduce two ways to develop Oracle Database applications using Visual C .

2. Develop database applications using Pro * C

2.1 Pro * C Working principle

The PRO series is a set of precompiled transcripts provided by Oracle in the third-generation advanced programming language to access the SQL statement, including Pro * ADA, Pro *, Pro * Cobol, Pro * Fortran, Pro * Pascal and PRO * PL / I. The programmer writes the PRO source that embeds the SQL statement with the corresponding advanced language (if the C language is called the Pro * C source program), the program is called the Pro * C source program, and the embedded SQL statement is converted into a standard Oracle call and generated. The target source program, that is, the source program of pure high-level language format, and then call these source programs to the user's program, the processing process is shown below.

Oracle Preciplators provide the following features:

(1) You can write an application in any of the six universal advanced programming languages.

(2) Follow the ANSI standard and embed the SQL statement in advanced languages.

(3) Dynamic SQL methods can be used to allow the program to accept or construct a valid SQL statement at runtime.

⑷ Realize the automatic conversion between Oracle internal data types and advanced language data types.

⑸ You can improve performance by embedding the PL / SQL transformation block in the application.

⑹能 You can specify the required precompiled options on the procedure line and command line and change their values ​​during the precompiled process.

⑺能 can fully check the grammar and semantics of embedded SQL data manipulation schemas and PL / SQL blocks.

⑻ The Oracle database that can be accessed with multiple locations in parallel with SQL * NET.

⑼ You can use the array as an input and output program variable.

⑽能 Conditions to code segments in the application.

⑾ provides a strong exception handling function.

It can be seen that by the combination of precompile procedures and other advanced languages, it can provide powerful means with SQL powerful functions and flexibility for the development of database application systems, but also make full use of high-level language itself in system development. Advantages provide a complete development solution based on Oracle database applications.

2.2 Using Pro * C in VC

Each Pro * C source file is typically composed of two parts: the program head and the program. The program header contains a host variable (the variable included in the SQL statement) description, the communication area definition, and the C-external representation, etc. Program is generally composed of several functions, including SQL statements (starting with Exec SQL).

Pro * c Supported data types include varchar2 (variable length strings), Number (two-in number), int GER, FLOAT, String (string with NULL), Varnum Number, long (viable long string), varchar (variable length string), ROWID (binary value), Date, Varraw, Varraw, RAW (fixed length binary data) LongRaw, unsigned (no symbol integer), longvarchar, longvarraw, char (pixel string), Charz (Class C) String), MLSLabel (becoming long binary data). You cannot use the 'L' or 'u' 作 词 or '0x' 作 头 修 饰 饰 作 作 作 作 语 语 号 语 号 号 号 号 号 号 号 号 号 号 号 号 号 号 号 号 号 号 号 号 号(Such as the table name, etc.); the address, indirect, bit logic, composite assignment, =, -, ,%, <<, >> operator is not allowed in the SQL statement and use NOT, AND, OR, = replace!, &&, ||, ==.

The following program is an example of the Pro * C source program of the coupling database.

#include // Declare SQL Communication Zone #include #include EXEC SQL BEGIN DECLARE Section; varchar username [20]; // Declaration Host variable varchar password [20]; VARCHAR DBNAME [20]; EXEC SQL End Declare Section; Void DB_Connect () {structure ((char *) Username.arr, "scott"); username.len = Strlen ((char *) username.arr); strcpy Char *) Password.arr, "Tiger"); Password.len = Strlen ((char *) password.arr); strcpy ((char *) DBNAME.ARR, "sundb"); dbname.len = Strlen ((char *) DBNAME.ARR); Exec SQL WHENEVER SQLERROR STOP; // Implicit Handling EXEC SQL Connect: UserName Identified by: Password Using: DBName; / * IF (Sqlca.sqlcode! = 0) // Explicit exception Handling { AFXMessageBox ("/ N with Oracle Database Connection Fails!"); Return;} * /} When using Pro * C in VC, first use Pro * C to write subroutines for the required operational database, then run Pro * C The compiler turns the Pro * C source to the corresponding CPP source program, insert the program into the user engineering file and describe the function in the module that needs to be called to the insert function, and then you can call it in this module. function.

3. Use the ODBC middleware to access the database

3.1 ODBC Working Principles

ODBC is an Open Database Connect, an open database interconnect, which is a unified interface standard proposed by Microsoft in 1991, a unified interface standard for accessing a database, is an intermediate between applications and database systems. It implements the operation of the database by using the driver corresponding to the application platform and the required database to interact with the application, avoiding the operation of directly calling with the database directly in the application, providing the independence of the database.

ODBC is mainly composed of drivers and driver managers. The driver is a module for supporting the ODBC function (usually a DLL under Win95), each driver corresponding to the corresponding database, and simply change the application when the application is transplanted from a database system to another The alias corresponding to the corresponding database system is set by the ODBC Manager. The driver manager (included in ODBC32.DLL) can be linked to all ODBC applications, which is responsible for managing the binding of the ODBC function in the application to the DLL. ODBC uses a hierarchical approach to manage the database, in each layer of the database communication structure, the ODBC introduces a public interface to solve the potential inconsistency, which is well solved based on the database. The relative independence of the system application is one of the important reasons for ODBC to achieve great success.

From the structure, ODBC is divided into single-type and multi-type.

(1) Single-type driver

Single-type drivers are between applications and databases, as data is as data like intermediaries, providing a unified data access method.

When the user performs a database operation, the application passes an ODBC function call to the ODBC driver manager, and it is determined by the ODBC API to process it directly and the result is returned or the driver is executed and the result is returned.

As can be seen, the homogeneous driver itself is a database engine, which can be done directly to the database, although the database may be located anywhere in the network.

(2) multi-type driver

The multi-type driver is responsible for transmitting commands and data between database engines and client applications, which do not perform data processing operations and an interface for network communication protocols for remote operations.

The front-end application proposes a request for database processing. The request forwards to the ODBC driver manager, the driver manager is done or transmits the multi-beam driver in accordance with the request, and the multi-beam driver will be translated into specific The manufacturer's database communication interface (such as Oracle's SQLNET) can understand and handle the interface to process, the interface transmits the request via the network to the server, then send the result back to the database communication interface, database The interface passes the result to the multi-style ODBC driver, and then transmits the result to the application.

3.2 Use ODBC in VC

Visual C provides five classes of CDatabase, CRecordset, CRecordView, CDBException, and CFieldView, CDBEXCEPTION, and CFIELDEXCHANGE, which encapsulates the ODBC SDK function, so that users can easily support the ODBC database without understanding the SDK function.

CDATABASE Class: Encapsulates and controls the method of submitting and rollback and executing a SQL statement.

CRecordset class: Most ways to encapsulate the manipulation of the database, including browsing, modifying records, control cursors, sorting.

CRecordView Class: Provides a view that is connected to the Recordset object, can establish a corresponding control and database data, and support mobile cursors, modify the record, etc.

CDBEXCEPTION Class: Provides an exception handling of database operations, and you can get a related return code for operation anomalies.

CFIELDEXCHANGE Class: Provides data exchange between user variables and database fields, and if you don't need to use a custom type, you will not need to call the function directly, the MFC Wizard will automatically establish a connection to programmers.

4. Comparison of two methods

转载请注明原文地址:https://www.9cbs.com/read-48347.html

New Post(0)