一个oci例子

来源:互联网 发布:java中的for循环 编辑:程序博客网 时间:2024/05/24 04:33

最小运行环境instantclient-basic-win32-10.1.0.5-20060419.zip中的oci.dll,oraociei10.dll

Instant Client Package下载地址:

http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/winsoft.html

 

#include "stdafx.h"
#include <oci.h>
#include <iostream>
#include <tchar.h>


//int _tmain(int argc, _TCHAR* argv[])
int main()
{
  
    char *user="name";
    char *password="123456";
    //char *dbName="test";

 char *dbName="(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 160.21.21.10)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = testdb)))";


 char *insertStmt = "insert into wc03m (wc03m03,wc03m04,wc03m02,wc03m19,wc03m03) values ('36','1000','002688',to_date('20100328','yyyymmdd'),'34333')";
   

 sword err;

    OCIEnv *envhpp;
    OCIError *errhpp;
    OCISvcCtx *svcCtx;
    OCIStmt *stmt;
    //创建环境句柄
    err = OCIEnvCreate(&envhpp,OCI_OBJECT,(dvoid*)0,(dvoid*(*)(dvoid*,size_t))0,(dvoid * (*)(dvoid *, dvoid *, size_t))0,
        (void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0 );
   
    //创建错误句柄
    err = OCIHandleAlloc(envhpp,(void**)&errhpp,OCI_HTYPE_ERROR,(size_t)0,(void**)0);
   
    //登陆服务器
    err = OCILogon(envhpp,errhpp,&svcCtx,(const OraText*)user,(ub4)strlen(user),(const OraText*)password,(ub4)strlen(password),
        (const OraText*)dbName,(ub4)strlen(dbName));

    //创建DML/DDL句柄
    err = OCIHandleAlloc(envhpp,(void**)&stmt,OCI_HTYPE_STMT,(size_t)0,(void**)0);
    //准备SQL语句
    err = OCIStmtPrepare(stmt,errhpp,(const OraText*)insertStmt,(ub4)strlen(insertStmt),OCI_NTV_SYNTAX,(ub4)OCI_DEFAULT);

    //执行SQL语句
    err = OCIStmtExecute(svcCtx,stmt,errhpp,(ub4)1,(ub4)0,(OCISnapshot*)0,(OCISnapshot*)0,OCI_DEFAULT);
    //提交当前事
    err = OCITransCommit(svcCtx,errhpp,0);
    return 0;

}