pro*c function Case 1--database connection, transaction processing

Source: Internet
Author: User

1. Program Structure
Each PRO*C program consists of two parts: (1) The application header, and (2) the application body
The application header defines the variables for the Oracle database and prepares to manipulate the Oracle database in the C language. The application body is basically made up of PRO*C SQL statement calls.
The main point is to query Select, INSERT, UPDATE, delete and other statements.
The composition of the application:

2. Function cases

EXEC SQL INCLUDE sqlca; #include"My_sys.h"#include"errlog.h"/*connecting to a database*/intOPENDB (CharDb_type)        {EXEC SQL BEGIN DECLARE section; Charsauser[ - ]; Charsapasswd[ - ]; Charsatnsname[ - ];    EXEC SQL END DECLARE section; /*********** * Online database ************/strcpy (Sauser, getenv ("Oracleuserid")); strcpy (SAPASSWD, getenv ("oracleuserpwd")); strcpy (Satnsname, getenv ("Oracletnsname"));    EXEC SQL Connect:sauser identified by:sapasswd using:satnsname; if(Sqlca.sqlcode! =0) {printf ("db not open[%d]\n", Sqlca.sqlcode); return-1 ; }    return 0;}/*Reconnect the database*/intreopendb () {intTry_num =0, ret; Htlog (ERROR,"DB down\n");    Closedb ();  while(1) {printf ("Connect to DB%dth\n", Try_num); RET= Opendb ('L'); if(ret = =0) {printf ("Connect to DB succ\n"); return 0;        } closedb (); Sleep ( -); Try_num++; if(Try_num > the)             Break ; } printf ("db not open\n"); return-1 ;}/*Close the database*/intClosedb () {EXEC SQL COMMIT work RELEASE; return 0 ;}/*Transaction Rollback*/intDbsrollback () {EXEC SQL ROLLBACK work; return 0;}/*Transaction Commit*/intDbscommit () {EXEC SQL COMMIT; return 0;}

Description
You need to configure the Oracleuserid, ORACLEUSERPWD, oracletnsname three environment variables in. BASRC before running. Where the printf part can be changed to T_log, E_log output

pro*c function Case 1--database connection, transaction processing

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.