c快速操作oracle数据库(c# 操作oracle)

C语言是一种高效的编程语言,被广泛应用于各种领域。当需要在C语言中操作Oracle数据库时,可以使用Oracle提供的OCI(Oracle Call Interface)库。OCI库提供了一组接口函数,可以通过C语言快速地连接和操作Oracle数据库。本文将介绍OCI库的使用方法,并给出相关的示例代码。

1. 连接数据库

在使用OCI库操作Oracle数据库之前,首先需要建立连接。这可以通过OCI库提供的函数来实现。以下是一个简单的示例程序:

#include 
#include
#include

int mn()
{
OCIEnv* envhp;
OCIError* errhp;
OCIServer* srvhp;
OCISession* seshp;
sword status;

status = OCIEnvCreate(&envhp, OCI_DEFAULT, (void*)0, (void*(*)(void*, size_t))0,
(void*(*)(void*, void*, size_t))0, (void(*)(void*, void*))0, 0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIEnvCreate fled!\n");
exit(EXIT_FLURE);
}

status = OCIHandleAlloc((void*)envhp, (void**)&errhp, OCI_HTYPE_ERROR, (size_t)0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIHandleAlloc fled!\n");
exit(EXIT_FLURE);
}

status = OCIHandleAlloc((void*)envhp, (void**)&srvhp, OCI_HTYPE_SERVER, (size_t)0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIHandleAlloc fled!\n");
exit(EXIT_FLURE);
}

status = OCIHandleAlloc((void*)envhp, (void**)&seshp, OCI_HTYPE_SESSION, (size_t)0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIHandleAlloc fled!\n");
exit(EXIT_FLURE);
}

status = OCILogon(envhp, errhp, &srvhp, (OraText*)"scott", (ub4)strlen("scott"), (OraText*)"tiger", (ub4)strlen("tiger"), (OraText*)"XE", (ub4)strlen("XE"));
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCILogon fled!\n");
exit(EXIT_FLURE);
}

return 0;
}

在这个程序中,我们使用OCIEnvCreate函数创建了一个OCI环境,然后使用OCIHandleAlloc函数分别创建了一个OCI错误句柄、OCI服务器句柄和OCI会话句柄。接着,使用OCILogon函数建立了到Oracle数据库的连接。在连接时需要指定用户名、密码和数据库实例名。

2. 执行SQL语句

建立连接之后,可以使用OCI库提供的函数执行SQL语句。以下是一个示例程序:

#include 
#include
#include

int mn()
{
OCIEnv* envhp;
OCIError* errhp;
OCIServer* srvhp;
OCISession* seshp;
OCIStmt* stmthp;
sword status;

status = OCIEnvCreate(&envhp, OCI_DEFAULT, (void*)0, (void*(*)(void*, size_t))0,
(void*(*)(void*, void*, size_t))0, (void(*)(void*, void*))0, 0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIEnvCreate fled!\n");
exit(EXIT_FLURE);
}

status = OCIHandleAlloc((void*)envhp, (void**)&errhp, OCI_HTYPE_ERROR, (size_t)0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIHandleAlloc fled!\n");
exit(EXIT_FLURE);
}

status = OCIHandleAlloc((void*)envhp, (void**)&srvhp, OCI_HTYPE_SERVER, (size_t)0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIHandleAlloc fled!\n");
exit(EXIT_FLURE);
}

status = OCIHandleAlloc((void*)envhp, (void**)&seshp, OCI_HTYPE_SESSION, (size_t)0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIHandleAlloc fled!\n");
exit(EXIT_FLURE);
}

status = OCILogon(envhp, errhp, &srvhp, (OraText*)"scott", (ub4)strlen("scott"), (OraText*)"tiger", (ub4)strlen("tiger"), (OraText*)"XE", (ub4)strlen("XE"));
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCILogon fled!\n");
exit(EXIT_FLURE);
}

status = OCIHandleAlloc((void*)envhp, (void**)&stmthp, OCI_HTYPE_STMT, (size_t)0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIHandleAlloc fled!\n");
exit(EXIT_FLURE);
}

status = OCIStmtPrepare(stmthp, errhp, (OraText*)"SELECT * FROM emp WHERE empno = :1", (ub4)strlen("SELECT * FROM emp WHERE empno = :1"), OCI_NTV_SYNTAX, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIStmtPrepare fled!\n");
exit(EXIT_FLURE);
}

int empno = 7369;
status = OCIBindByPos(stmthp, (OCIBind**)&(empno), sizeof(int), SQLT_INT, (dvoid*)0, (ub2*)0, (ub2*)0, 0, (ub4*)0, 0, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIBindByPos fled!\n");
exit(EXIT_FLURE);
}

status = OCIStmtExecute(seshp, stmthp, errhp, (ub4)1, (ub4)0, (OCISnapshot*)0, (OCISnapshot*)0, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIStmtExecute fled!\n");
exit(EXIT_FLURE);
}

OCIStmt* resulthp = 0;
status = OCIHandleAlloc((void*)envhp, (void**)&resulthp, OCI_HTYPE_STMT, (size_t)0, (void**)0);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIHandleAlloc fled!\n");
exit(EXIT_FLURE);
}

status = OCIStmtFetch2(stmthp, errhp, 1, OCI_FETCH_NEXT, 0, OCI_DEFAULT);
if (status != OCI_SUCCESS && status != OCI_NO_DATA)
{
fprintf(stderr, "OCIStmtFetch2 fled!\n");
exit(EXIT_FLURE);
}

if (status == OCI_SUCCESS)
{
int empno = 0;
char ename[11] = { 0 };
char job[10] = { 0 };
int mgr = 0;
char hiredate[12] = { 0 };
int sal = 0;
int comm = 0;
int deptno = 0;

OCIParam* paramhp;
status = OCIAttrGet(stmthp, OCI_HTYPE_STMT, (void*)&paramhp, (ub4*)0, OCI_ATTR_PARAM, errhp);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIAttrGet fled!\n");
exit(EXIT_FLURE);
}

status = OCIParamGet(paramhp, OCI_DTYPE_PARAM, errhp, (void**)&resulthp, 1);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIParamGet fled!\n");
exit(EXIT_FLURE);
}

for (;;)
{
int i = 1;
status = OCIStmtFetch2(stmthp, errhp, 1, OCI_FETCH_NEXT, 0, OCI_DEFAULT);
if (status == OCI_NO_DATA)
{
break;
}

status = OCIAttrGet(resulthp, OCI_DTYPE_PARAM, &empno, 0, OCI_ATTR_DATA_TYPE, errhp);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIAttrGet fled!\n");
exit(EXIT_FLURE);
}

status = OCIAttrGet(resulthp, OCI_DTYPE_PARAM, ename, &i, OCI_ATTR_DATA_TYPE, errhp);
if (status != OCI_SUCCESS)
{
fprintf(stderr, "OCIAttrGet fled!\n");
exit(EXIT_FLURE);
}

status = OCIAttrGet(resulthp, OCI_DTYPE_PARAM, job, &i, OCI_ATTR_DATA

数据运维技术 » c快速操作oracle数据库(c# 操作oracle)