PROC simple use case -- connect ORACLE with VC
Operating System: windows 7
Database Version: oracle 10g
VS version: VS2010
There are many ways to connect to ORACLE. Here, we only use PROC as an example to explain how to connect to oracle. What's wrong with it? I hope you can learn more.
1. Install the Client Version corresponding to oracle 10 Gb.
Note: Take the local machine as an example. The D: \ oracle \ product \ 10.2.0 \ client_2 \ NETWORK \ ADMIN \ tnsnames. ora file is
# Tnsnames. ora Network Configuration File: d: \ oracle \ product \ 10.2.0 \ client_2 \ network \ admin \ tnsnames. ora
# Generated by Oracle configuration tools.
DB_DEMO =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP) (HOST = 10.1.8.222) (PORT = 1521 ))
)
(CONNECT_DATA =
(SERVICE_NAME = db_demo)
)
)
Host: IP address of the ORACLE server; PORT: PORT Number of the ORACLE server. The default value is 1521. SERVICE_NAME: Database Name.
2. Create an oracle. pc file with the following content:
// Oracle. cpp: Defines the entry point for the console application. // # include <stdio. h> # include <stdlib. h> # include <process. h> # include <errno. h> // exec SQL INCLUDE SQLCA; # include <sqlca. h> int main (int argc, char * argv []) {exec SQL BEGIN DECLARE SECTION; VARCHAR myusername [20], mypassword [20], myserver [20]; exec SQL END DECLARE SECTION; printf ("\ n enter the User name:"); gets (char *) myusername. arr); myusername. le N = (unsigned short) strlen (char *) myusername. arr); printf ("\ n enter the password:"); gets (char *) mypassword. arr); mypassword. len = (unsigned short) strlen (char *) mypassword. arr); printf ("\ n enter the server name:"); gets (char *) myserver. arr); myserver. len = (unsigned short) strlen (char *) myserver. arr); exec SQL CONNECT: myusername IDENTIFIED BY: mypassword USING: myserver; if (sqlca. sqlcode <0) printf ("\ n USER % s successfully connected to the server % S! \ N ", myusername. arr, myserver. arr); elseprintf ("\ n % ld, % s \ n", sqlca. sqlcode, (char *) sqlca. sqlerrm. sqlerrmc); return 0 ;}
3. Find the proc.exe file in the oracleinstaller. The local directory is D: \ oracle \ product \ 10.2.0 \ client_2 \ export oracle. pc oracle. cpp. The oracle. cpp file is generated in the current directory.
4. Start VS2010 and create an oracle project with an empty control platform. Pay attention to the following: configure the VS project. Copy oracle. cpp to the oracle directory and add it to the project. Then configure:
1. database include header file path Configuration
2. Database LIB file directory Configuration
3. The database depends on the LIB file configuration
5. Delete the previously automatically generated oracle. cpp file, add oracle. cpp to the oracle project, and compile the link. The following errors may occur:
1. The system prompts that "stdafx. h" is not included ". Solution: Add # include "stdafx. h" at the beginning of oracle. cpp"
2. error LNK2001: unresolved external symbol "void _ cdecl sqlcxt (void **, unsigned int *, struct sqlexd *, struct sqlcxp const *)"(? Sqlcxt @ YAXPAPAXPAIPAUsqlexd @ PBUsqlcxp @ Z) error. Solution: extern void sqlcxt (void **, unsigned int *, struct sqlexd *, const struct sqlcxp *); Add "C" after extern to compile
3. For other problems, check whether the configured include and lib paths are correct and whether the lib file name is correct.
Oracle.exe generated by running the statement in Step 6. As follows: