码迷,mamicode.com
首页 > 数据库 > 详细

数据库 Proc编程二

时间:2017-01-12 12:07:07      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:分配   使用   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/123456@orcl";
    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");
}

 

数据库 Proc编程二

标签:分配   使用   can   war   crt   blog   close   std   connect   

原文地址:http://www.cnblogs.com/zhanggaofeng/p/6275493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!