#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include "sqlca.h" //连接数据库 /* EXEC SQL CONNECT {:user IDENTIFIED BY :oldpswd | :usr_psw } [[AT { dbname | :host_variable }] USING :connect_string ] [ { ALTER AUTHORIZATION :newpswd | IN { SYSDBA | SYSOPER } MODE }]; */ EXEC SQL BEGIN DECLARE SECTION; char * serverid="scott/[email protected]"; char user[32]; char passwd[32]; char sid[32]; EXEC SQL END DECLARE SECTION; //user 用户名 //passwd 密码 //sid 数据库ID void main() { int ret=0; //C语言中使用宿主变量 //printf("serverid=%s\r\n",serverid); printf("user:"); scanf("%s",user); printf("passwd:"); scanf("%s",passwd); printf("sid:"); scanf("%s",sid); //连接数据库方式一--在嵌入SQL编程中使用变量需要加: EXEC SQL connect:user IDENTIFIED BY :passwd USING :sid ; //连接数据库方式二 //EXEC SQL connect:serverid; if(sqlca.sqlcode!=0) { ret=sqlca.sqlcode; printf("connect err :%d",ret); system("pause"); }else { printf("connect ok!\r\n"); //提交事务断开连接 EXEC SQL commit release; printf("Oracle closed !\r\n"); system("pause"); } }
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include "sqlca.h" EXEC SQL BEGIN DECLARE SECTION; char *user="scott"; char *passwd="123456"; char *sid="orcl"; char *linkf="link1";//通过宿主变量 指定名字 char *linkt="link2"; EXEC SQL END DECLARE SECTION; //user 用户名 //passwd 密码 //sid 数据库ID void main() { int ret=0; //连接数据库方式三 --多个用户连接多个数据库 EXEC SQL connect:user IDENTIFIED BY :passwd AT :linkf USING :sid ; if(sqlca.sqlcode!=0) { ret=sqlca.sqlcode; printf("connect1 err :%d",ret); system("pause"); }else { printf("connect1 ok!\r\n"); //提交事务断开连接 EXEC SQL commit release; printf("Oracle closed !\r\n"); //system("pause"); } EXEC SQL connect:user IDENTIFIED BY :passwd AT :linkt USING :sid ; if(sqlca.sqlcode!=0) { ret=sqlca.sqlcode; printf("connect2 err :%d",ret); system("pause"); }else { printf("connect2 ok!\r\n"); //提交事务断开连接 EXEC SQL commit release; printf("Oracle closed !\r\n"); //system("pause"); } system("pause"); }
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include "sqlca.h" //这里我用的是一个用户,其实也可以是多个用户 EXEC SQL BEGIN DECLARE SECTION; char *user="scott"; char *passwd="123456"; char *sid="orcl"; EXEC SQL END DECLARE SECTION; void main() { int ret=0; //和第三种方式不同,由oracle自动分配 EXEC SQL DECLARE linkf DATABASE; EXEC SQL DECLARE linkt DATABASE; //连接数据库方式四 --注意linkf不再是宿主变量,所以不需要加: EXEC SQL connect:user IDENTIFIED BY :passwd AT linkf USING :sid ; if(sqlca.sqlcode!=0) { ret=sqlca.sqlcode; printf("connect1 err :%d",ret); system("pause"); }else { printf("connect1 ok!\r\n"); //提交事务断开连接 EXEC SQL commit release; printf("Oracle closed !\r\n"); //system("pause"); } EXEC SQL connect:user IDENTIFIED BY :passwd AT linkt USING :sid ; if(sqlca.sqlcode!=0) { ret=sqlca.sqlcode; printf("connect2 err :%d",ret); system("pause"); }else { printf("connect2 ok!\r\n"); //提交事务断开连接 EXEC SQL commit release; printf("Oracle closed !\r\n"); //system("pause"); } system("pause"); }
时间: 2024-10-11 16:53:34