环境:

WinXPSP2 + VC6 + ORACLE10g

步骤:

1)新建demo.pc,内容如下:

#include <stdio.h> #include <string.h> typedef char asciz; EXEC SQL BEGIN DECLARE SECTION; EXEC SQL TYPE asciz IS STRING(20); asciz username[20]; asciz password[20]; int dept_number; char dept_name[50]; char location[50]; EXEC SQL END DECLARE SECTION; EXEC SQL INCLUDE sqlca; main() { strcpy(username,"SCOTT"); strcpy(password,"x"); EXEC SQL WHENEVER SQLERROR GOTO sql_error; EXEC SQL CONNECT :username IDENTIFIED BY :password; printf("/n Connect to ORACLE"); while(1) { printf("/n Enter department Number(0 to end)?"); scanf("%d",&dept_number); if(dept_number==0) { EXEC SQL COMMIT WORK RELEASE; printf("/n Exiting program"); exit(0); } //---------------PLSQL块开始--------------- EXEC SQL EXECUTE BEGIN SELECT DNAME,LOC INTO :dept_name,:location FROM DEPT WHERE DEPTNO=:dept_number; // NULL; --IS OK END; END-EXEC; //---------------PLSQL块结束--------------- printf("/n Dept No./t Dept Name/t Location"); printf("/n ------------------------------------"); printf("/n %d/t%s/t%s",dept_number,dept_name,location); } sql_error: EXEC SQL WHENEVER SQLERROR CONTINUE; EXEC SQL ROLLBACK WORK RELEASE; printf("/n processing error!"); printf("/n Exiting program!"); exit(1); }

2)将demo.pc预编译为demo.c:

proc demo.pc SQLCHECK=SEMANTICS userid="scott/x@orcl"  

3)将demo.c编译为demo.obj:

cl demo.c /nologo /c /I E:/oracle/product/10.2.0/db_1/precomp/public

4)将demo.obj链接为demo.exe:

link demo.obj /nologo /LIBPATH:E:/oracle/product/10.2.0/db_1/precomp/LIB orasql10.lib

5)运行demo.exe进行测试。