C操作Oracle数据库实现图片存取(c# oracle 图片)

C操作Oracle数据库实现图片存取

在实际的开发中,我们经常需要在数据库中存储图片、音频、视频等大型二进制数据,而Oracle数据库是比较常用的关系型数据库之一。本文将介绍如何使用C语言操作Oracle数据库来实现图片的存储和读取。

1. 准备工作

在使用C语言操作Oracle数据库之前,需要安装Oracle客户端和Oracle ODBC驱动程序。安装完成后,需要设置一些环境变量:

– ORACLE_HOME:Oracle客户端的安装目录

– PATH:将Oracle客户端的bin目录添加到PATH环境变量中

– TNS_ADMIN:如果使用TNS描述符连接数据库,则需要设置该环境变量,值为TNS描述符所在的目录

2. 创建表结构

在Oracle数据库中,可以使用BLOB类型来存储大型二进制数据。下面是一个示例表结构:

CREATE TABLE image (

id NUMBER(10) NOT NULL,

name VARCHAR2(100) NOT NULL,

image_data BLOB,

CONSTRNT image_pk PRIMARY KEY (id)

);

3. 存储图片

下面是一个示例代码,用于将一张图片存储到Oracle数据库中:

#include

#include

const char* image_file = “test.jpg”;

const char* dsn = “ORACLE”;

int mn() {

OCIEnv* envhp;

OCIError* errhp;

OCISvcCtx* svchp;

OCIServer* srvhp;

OCISession* sesshp;

OCIStmt* stmthp;

OCILobLocator* lob = NULL;

FILE* fp;

int r, id;

// 初始化OCI环境

r = OCIEnvCreate(&envhp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL);

if (r != OCI_SUCCESS) {

printf(“OCIEnvCreate fled (%d)\n”, r);

return -1;

}

// 分配OCI错误句柄

r = OCIHandleAlloc(envhp, (void**)&errhp, OCI_HTYPE_ERROR, 0, NULL);

if (r != OCI_SUCCESS) {

printf(“OCIHandleAlloc (error) fled (%d)\n”, r);

return -1;

}

// 分配OCI服务上下文句柄

r = OCIHandleAlloc(envhp, (void**)&svchp, OCI_HTYPE_SVCCTX, 0, NULL);

if (r != OCI_SUCCESS) {

printf(“OCIHandleAlloc (service) fled (%d)\n”, r);

return -1;

}

// 分配OCI服务器句柄

r = OCIHandleAlloc(envhp, (void**)&srvhp, OCI_HTYPE_SERVER, 0, NULL);

if (r != OCI_SUCCESS) {

printf(“OCIHandleAlloc (server) fled (%d)\n”, r);

return -1;

}

// 分配OCI会话句柄

r = OCIHandleAlloc(envhp, (void**)&sesshp, OCI_HTYPE_SESSION, 0, NULL);

if (r != OCI_SUCCESS) {

printf(“OCIHandleAlloc (session) fled (%d)\n”, r);

return -1;

}

// 创建OCI服务器

r = OCIServerAttach(srvhp, errhp, (text*)dsn, strlen(dsn), OCI_DEFAULT);

if (r != OCI_SUCCESS) {

printf(“OCIHandleAlloc (session) fled (%d)\n”, r);

return -1;

}

// 设置OCI服务上下文句柄属性

r = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, srvhp, 0, OCI_ATTR_SERVER, errhp);

if (r != OCI_SUCCESS) {

printf(“OCIAttrSet (server) fled (%d)\n”, r);

return -1;

}

// 设置OCI会话句柄属性

r = OCIAttrSet(sesshp, OCI_HTYPE_SESSION, (void*)”username”, strlen(“username”), OCI_ATTR_USERNAME, errhp);

if (r != OCI_SUCCESS) {

printf(“OCIAttrSet (username) fled (%d)\n”, r);

return -1;

}

r = OCIAttrSet(sesshp, OCI_HTYPE_SESSION, (void*)”password”, strlen(“password”), OCI_ATTR_PASSWORD, errhp);

if (r != OCI_SUCCESS) {

printf(“OCIAttrSet (password) fled (%d)\n”, r);

return -1;

}

// 启动OCI会话

r = OCISessionBegin(svchp, errhp, sesshp, OCI_CRED_RDBMS, OCI_DEFAULT);

if (r != OCI_SUCCESS) {

printf(“OCISessionBegin fled (%d)\n”, r);

return -1;

}

// 设置OCI服务上下文句柄属性

r = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, sesshp, 0, OCI_ATTR_SESSION, errhp);

if (r != OCI_SUCCESS) {

printf(“OCIAttrSet (session) fled (%d)\n”, r);

return -1;

}

// 准备SQL语句

r = OCIHandleAlloc(envhp, (void**)&stmthp, OCI_HTYPE_STMT, 0, NULL);

if (r != OCI_SUCCESS) {

printf(“OCIHandleAlloc (statement) fled (%d)\n”, r);

return -1;

}

r = OCIStmtPrepare(stmthp, errhp, (text*)”INSERT INTO image (id, name, image_data) VALUES (:1, :2, :3)”, strlen(“INSERT INTO image (id, name, image_data) VALUES (:1, :2, :3)”), OCI_NTV_SYNTAX, OCI_DEFAULT);

if (r != OCI_SUCCESS) {

printf(“OCIStmtPrepare fled (%d)\n”, r);

return -1;

}

// 绑定变量

r = OCIBindByName(stmthp, (OCIBind**)&lob, errhp, (text*)”:3″, strlen(“:3”), (void**)NULL, 0, SQLT_BLOB, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);

if (r != OCI_SUCCESS) {

printf(“OCIBindByName (lob) fled (%d)\n”, r);

return -1;

}

r = OCIBindByName(stmthp, (OCIBind**)&id, errhp, (text*)”:1″, strlen(“:1”), (void*)&id, sizeof(int), SQLT_INT, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);

if (r != OCI_SUCCESS) {

printf(“OCIBindByName (id) fled (%d)\n”, r);

return -1;

}

r = OCIBindByName(stmthp, (OCIBind**)&name, errhp, (text*)”:2″, strlen(“:2”), (void*)name, sizeof(name), SQLT_STR, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);

if (r != OCI_SUCCESS) {

printf(“OCIBindByName (name) fled (%d)\n”, r);

return -1;

}

// 执行SQL语句

r = OCIStmtExecute(svchp, stmthp, errhp, 1, 0, NULL, NULL, OCI_DEFAULT);

if (r != OCI_SUCCESS) {

printf(“OCIStmtExecute fled (%d)\n”, r);

return -1;

}

// 读取图片数据

fp = fopen(image_file, “rb”);

if (fp == NULL) {

printf(“Fled to open file: %s\n”, image_file);

return -1;

}

fseek(fp, 0, SEEK_END);

int size = ftell(fp);

fseek(fp, 0, SEEK_SET);

char* data = malloc(size);

fread(data, size, 1, fp);

fclose(fp);

// 写入LOB

r = OCILobCreateTemporary(svchp, errhp, &lob, OCI_DEFAULT, SQLT_BLOB, OCI_ATTR_LOB_STORAGE);

if (r != OCI_SUCCESS) {

printf(“OCILobCreateTemporary fled (%d)\n”, r);

return -1;

}

r = OCILobWrite(svchp, errhp, lob, &size, 1, data, size, OCI_ONE_PIECE, NULL, NULL, 0, SQLCS_IMPLICIT);

if (r != OCI_SUCCESS) {

printf(“OCILobWrite fled (%d)\n”, r);

return -1;

}

// 提交事务

r = OCITransCommit(svchp, errhp, OCI_DEFAULT);

if (r != OCI_SUCCESS) {

printf(“OCITransCommit fled (%d)\n”, r);

return -1;

}

printf(“Image has been saved to database.\n”);

// 释放资源

OCIHandleFree(lob, OCI_HTYPE_LOB


数据运维技术 » C操作Oracle数据库实现图片存取(c# oracle 图片)