標籤:分配 使用 can war crt blog close std connect
#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 資料庫IDvoid 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 資料庫IDvoid 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");}
資料庫 Proc編程二