数据库 proc编程三

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlca.h"


EXEC SQL BEGIN DECLARE SECTION;
    char *serverid="scott/123456@orcl";
    int deptid;
    char edname[32];
    char edloc[32];
EXEC SQL END DECLARE SECTION;

void main()
{
    int ret=0;
    EXEC SQL connect:serverid ;
    if(sqlca.sqlcode!=0)
    {
        ret=sqlca.sqlcode;
        printf("connect err :%d",ret);
        system("pause");
    }else
    {
        printf("connect ok!\r\n");
        //赋值
        deptid=97;
        strcpy(edname,"中国2");
        strcpy(edloc,"广东");
        //插入数据
        printf("exec insert start !\n");
        EXEC SQL insert into dept(DEPTNO,DNAME,LOC) values(:deptid,:edname,:edloc);
        if(sqlca.sqlcode!=0)
        {
            ret=sqlca.sqlcode;
            printf("insert err :%d",ret);
            system("pause");
            return;
        }
        //注意每次执行DML操作都需要提交事务不断开连接
        EXEC SQL commit;
        getchar();
        strcpy(edname,"80name");
        strcpy(edloc,"guangdong");
        //修改数据
        EXEC SQL update dept set DNAME=:edname,LOC=:edloc where DEPTNO=:deptid;
        //提交事务不断开连接
        EXEC SQL commit;
        printf("print any key delete !\n");
        getchar();
        //删除数据
        EXEC SQL delete from dept where deptno=:deptid;
        //提交事务断开连接
        EXEC SQL commit release;
        printf("Oracle closed !\r\n");
        system("pause");
    }
}

 

posted on 2017-01-12 14:40  寒魔影  阅读(261)  评论(0编辑  收藏  举报

导航