Pro*C/C++同时支持C和C++,但是当预编译包含C++代码的PC源程序时,必须合理地设置相关的预编译选项。
代码:xx.pc
/*功能:演示了Pro*C/C++源程序生成C++源文件说明:Pro*C/C++支持C和C++。当预编译包含C++代码的PC源程序时,必须合理地设置相关的预编译选项。预编译选项应该设置为cpp_suffix=cpp code=cpp parse=none(或partial)code = ANSI_C, KR_C, CPPparse = NONE, PARTIAL, FULLcpp_suffix = c, cpp, cc*/#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>EXEC SQL BEGIN DECLARE SECTION; // 定义宿主变量struct empdat{varchar emp_name[10];float salary;float commission;}emprec;struct empind{short emp_name_ind;short sal_ind;short comm_ind;}emprec_ind;int eno;
EXEC SQL END DECLARE SECTION;class emp // 自定义类
{char ename[10];float salary;float commission;public:emp();emp(empdat &dat, empind &ind);void setvalues(empdat &dat, empind &ind);friend ostream& operator<<(ostream &s, emp &e); // 定义友元函数
};emp::emp()
{memset(ename, 0, sizeof(ename));salary = commission = 0.0;
}emp::emp(empdat &dat, empind &ind)
{strncpy(ename, (char*)dat.emp_name.arr, dat.emp_name.len);ename[dat.emp_name.len] = '\0';this->salary = dat.salary;this->commission = (ind.comm_ind < 0) ? 0 : dat.commission;
}void emp::setvalues(empdat &dat, empind &ind)
{strncpy(ename, (char*)dat.emp_name.arr, dat.emp_name.len);ename[dat.emp_name.len] = '\0';this->salary = dat.salary;this->commission = (ind.comm_ind < 0) ? 0 : dat.commission;
}ostream& operator <<(ostream &s, emp &e)
{return s<<"name: "<<e.ename<<", salary: "<<e.salary<<", comm: "<<e.commission<<endl<<endl;
}#include <sqlca.h>
#pragma comment(lib, "orasql10.lib")int connect();
void sql();
void sql_error();void main()
{ EXEC SQL WHENEVER SQLERROR DO sql_error(); // 安装错误处理句柄 if(connect() == 0) { sql(); EXEC SQL COMMIT WORK RELEASE; // 提交事务,断开连接 } else printf("连接失败\n");
} int connect() // connect to oracle database
{ EXEC SQL BEGIN DECLARE SECTION; // 定义宿主变量char username[10], password[10], server[10]; EXEC SQL END DECLARE SECTION;strcpy(username, "scott"); strcpy(password, "scott"); strcpy(server, "orcl"); EXEC SQL CONNECT :username IDENTIFIED BY :password USING :server;if(sqlca.sqlcode == 0) return 0; else return sqlca.sqlcode;
} void sql_error() // print error infomation
{ printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
} void sql()
{ emp e;while(1){cout<<"请输入雇员号(0:退出):";flush(cout);scanf("%d", &eno);fflush(stdin);if(eno == 0)return;EXEC SQL WHENEVER NOT FOUND goto notfound; // 游标数据提取完毕后退出循环EXEC SQL select ename, sal, comm into :emprec indicator :emprec_ind from emp where empno=:eno;e.setvalues(emprec, emprec_ind);cout<<e;continue;notfound:cout<<"无效雇员号"<<endl<<endl;}printf("sqlca.sqlerrd[2] = %d\n", sqlca.sqlerrd[2]); // sqlca.sqlerrd[2]存放着Select语句作用的行数
}
xx.pc文件对应的预编译选项如图: