Oracle LOB

Oracle .NET Framework 数据提供程序包括 OracleLob 类,该类用于使用 Oracle LOB 数据类型。

OracleLob 可能是下列 OracleType 数据类型之一:

 

数据类型

说明

Blob

包含二进制数据的 Oracle BLOB 数据类型,其最大大小为 4 GB。 此数据类型映射到 Byte 类型的 Array

Clob

包含字符数据的 Oracle CLOB 数据类型,根据服务器的默认字符集,其最大大小为 4 GB。 此数据类型映射到 String

NClob

包含字符数据的 Oracle NCLOB 数据类型,根据服务器的区域字符集,其最大大小为 4G 字节。 此数据类型映射到 String

OracleLobOracleBFile 的区别在于前者的数据存储在服务器上而不是存储在操作系统的物理文件中。 它也可以是一个读写对象,这一点与 OracleBFile 不同(后者始终为只读)。

以下 C# 示例演示如何在 Oracle 表中创建 LOB,然后以 OracleLob 对象的形式检索并写入。 该示例演示如何使用 OracleDataReader 对象以及 OracleLobReadWrite 方法。 该示例使用 Oracle BLOBCLOBNCLOB 数据类型。

[C#]

 
 
using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.OracleClient; // LobExample
public class LobExample
{
public static int Main(string[] args)
{
//Create a connection.
OracleConnection conn = new OracleConnection(
"Data Source=Oracle8i;Integrated Security=yes");
using(conn)
{
//Open a connection.
conn.Open();
OracleCommand cmd = conn.CreateCommand(); //Create the table and schema.
CreateTable(cmd); //Read example.
ReadLobExample(cmd); //Write example
WriteLobExample(cmd);
} return 1;
} // ReadLobExample
public static void ReadLobExample(OracleCommand cmd)
{
int actual = 0; // Table Schema:
// "CREATE TABLE tablewithlobs (a int, b BLOB, c CLOB, d NCLOB)";
// "INSERT INTO tablewithlobs values (1, 'AA', 'AAA', N'AAAA')";
// Select some data.
cmd.CommandText = "SELECT * FROM tablewithlobs";
OracleDataReader reader = cmd.ExecuteReader();
using(reader)
{
//Obtain the first row of data.
reader.Read(); //Obtain the LOBs (all 3 varieties).
OracleLob blob = reader.GetOracleLob(1);
OracleLob clob = reader.GetOracleLob(2);
OracleLob nclob = reader.GetOracleLob(3); //Example - Reading binary data (in chunks).
byte[] buffer = new byte[100];
while((actual = blob.Read(buffer, 0, buffer.Length)) >0)
Console.WriteLine(blob.LobType + ".Read(" + buffer + ", " +
buffer.Length + ") => " + actual); // Example - Reading CLOB/NCLOB data (in chunks).
// Note: You can read character data as raw Unicode bytes
// (using OracleLob.Read as in the above example).
// However, because the OracleLob object inherits directly
// from the .Net stream object,
// all the existing classes that manipluate streams can
// also be used. For example, the
// .Net StreamReader makes it easier to convert the raw bytes
// into actual characters.
StreamReader streamreader =
new StreamReader(clob, Encoding.Unicode);
char[] cbuffer = new char[100];
while((actual = streamreader.Read(cbuffer,
0, cbuffer.Length)) >0)
Console.WriteLine(clob.LobType + ".Read(
" + new string(cbuffer, 0, actual) + ", " +
cbuffer.Length + ") => " + actual); // Example - Reading data (all at once).
// You could use StreamReader.ReadToEnd to obtain
// all the string data, or simply
// call OracleLob.Value to obtain a contiguous allocation
// of all the data.
Console.WriteLine(nclob.LobType + ".Value => " + nclob.Value);
}
} // WriteLobExample
public static void WriteLobExample(OracleCommand cmd)
{
//Note: Updating LOB data requires a transaction.
cmd.Transaction = cmd.Connection.BeginTransaction(); // Select some data.
// Table Schema:
// "CREATE TABLE tablewithlobs (a int, b BLOB, c CLOB, d NCLOB)";
// "INSERT INTO tablewithlobs values (1, 'AA', 'AAA', N'AAAA')";
cmd.CommandText = "SELECT * FROM tablewithlobs FOR UPDATE";
OracleDataReader reader = cmd.ExecuteReader();
using(reader)
{
// Obtain the first row of data.
reader.Read(); // Obtain a LOB.
OracleLob blob = reader.GetOracleLob(1/*0:based ordinal*/); // Perform any desired operations on the LOB
// (read, position, and so on). // Example - Writing binary data (directly to the backend).
// To write, you can use any of the stream classes, or write
// raw binary data using
// the OracleLob write method. Writing character vs. binary
// is the same;
// however note that character is always in terms of
// Unicode byte counts
// (for example, even number of bytes - 2 bytes for every
// Unicode character).
byte[] buffer = new byte[100];
buffer[0] = 0xCC;
buffer[1] = 0xDD;
blob.Write(buffer, 0, 2);
blob.Position = 0;
Console.WriteLine(blob.LobType + ".Write(
" + buffer + ", 0, 2) => " + blob.Value); // Example - Obtaining a temp LOB and copying data
// into it from another LOB.
OracleLob templob = CreateTempLob(cmd, blob.LobType);
long actual = blob.CopyTo(templob);
Console.WriteLine(blob.LobType + ".CopyTo(
" + templob.Value + ") => " + actual); // Commit the transaction now that everything succeeded.
// Note: On error, Transaction.Dispose is called
// (from the using statement)
// and will automatically roll back the pending transaction.
cmd.Transaction.Commit();
}
} // CreateTempLob
public static OracleLob CreateTempLob(
OracleCommand cmd, OracleType lobtype)
{
//Oracle server syntax to obtain a temporary LOB.
cmd.CommandText = "DECLARE A " + lobtype + "; "+
"BEGIN "+
"DBMS_LOB.CREATETEMPORARY(A, FALSE); "+
":LOC := A; "+
"END;"; //Bind the LOB as an output parameter.
OracleParameter p = cmd.Parameters.Add("LOC", lobtype);
p.Direction = ParameterDirection.Output; //Execute (to receive the output temporary LOB).
cmd.ExecuteNonQuery(); //Return the temporary LOB.
return (OracleLob)p.Value;
} // CreateTable
public static void CreateTable(OracleCommand cmd)
{
// Table Schema:
// "CREATE TABLE tablewithlobs (a int, b BLOB, c CLOB, d NCLOB)";
// "INSERT INTO tablewithlobs VALUES (1, 'AA', 'AAA', N'AAAA')";
try
{
cmd.CommandText = "DROP TABLE tablewithlobs";
cmd.ExecuteNonQuery();
}
catch(Exception)
{
} cmd.CommandText =
"CREATE TABLE tablewithlobs (a int, b BLOB, c CLOB, d NCLOB)";
cmd.ExecuteNonQuery();
cmd.CommandText =
"INSERT INTO tablewithlobs VALUES (1, 'AA', 'AAA', N'AAAA')";
cmd.ExecuteNonQuery();
}
}

以下 C# 示例演示如何创建临时 LOB。

[C#]

 
 
OracleConnection conn = new OracleConnection(
"server=test8172; integrated security=yes;");
conn.Open(); OracleTransaction tx = conn.BeginTransaction(); OracleCommand cmd = conn.CreateCommand();
cmd.Transaction = tx;
cmd.CommandText =
"declare xx blob; begin dbms_lob.createtemporary(
xx, false, 0); :tempblob := xx; end;";
cmd.Parameters.Add(new OracleParameter("tempblob",
OracleType.Blob)).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
OracleLob tempLob = (OracleLob)cmd.Parameters[0].Value;
tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
tempLob.Write(tempbuff,0,tempbuff.Length);
tempLob.EndBatch();
cmd.Parameters.Clear();
cmd.CommandText = "myTable.myProc";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter(
"ImportDoc", OracleType.Blob)).Value = tempLob;
cmd.ExecuteNonQuery(); tx.Commit();

Oracle LOB的更多相关文章

  1. oracle之 Oracle LOB 详解

    一.  官方说明 Oracle 11gR2 文档: LOB Storage http://download.oracle.com/docs/cd/E11882_01/appdev.112/e18294 ...

  2. php 操作 oracle lob 数据

    http://www.oracle.com/technetwork/articles/fuecks-lobs-095315.html Working with LOBs in Oracle and P ...

  3. [Oracle] Lob介绍

    [Oracle] Lob介绍   像Oracle这种关系型数据库,比较擅长处理结构化的数据,那么对于非结构化的数据,Oracle是怎么处理和存储的呢?Lob (Large Object)是Oracle ...

  4. [下载]Oracle LOB字段编辑工具

    OraLobEditor 是Oracle LOB (CLOB, BLOB) 字段编辑工具. 查看.编辑LOB (CLOB, BLOB)字段(plain text, RTF, image, hex, h ...

  5. Oracle LOB 大对象处理

    LOB类型列主要是用来存储大量数据的数据库字段,最大可以存储4G字节的非结构化数据. 一.LOB数据类型分类 1.按存储数据的类型分: ①字符类型:   CLOB:存储大量 单字节 字符数据.   N ...

  6. Oracle LOB类型

    一.Oracle中的varchar2类型1.我们在Oracle数据库存储的字符数据一般是用VARCHAR2.VARCHAR2既分PL/SQL Data Types中的变量类型,也分Oracle Dat ...

  7. oracle Lob对象空间回收测试

    备注:转自网络 SQL> create table t(time date,text clob);SQL> begin for i in 1 .. 10000 loop insert in ...

  8. php 操作 oracle lob 数据2

    CREATE SEQUENCE mylobs_id_seq    NOMINVALUE    NOMAXVALUE    NOCYCLE    CACHE 20    NOORDERINCREMENT ...

  9. Oracle LOB字段判空

    dbms_lob.getlength() dbms_lob.getlength(null) 会报错--- Oracle 默认为clob字段插入empty_clob()

随机推荐

  1. 在C#中使用正则表达式自动匹配并获取所需要的数据

    转自:http://my.oschina.net/bv10000/blog/111736 正则表达式能根据设置匹配各种数据(比如:e-mail地址,电话号码,身份中号码等等).正则表达式功能强大,使用 ...

  2. linux下安装多个mysql实例(摘自国外:How to create multiple mysql instance in CentOS 6.4 and Red Hat 6.4)

    How to create multiple mysql instance in CentOS 6.4 and Red Hat 6.4 from:http://sharadchhetri.com/20 ...

  3. 第一章JSP基础语法

    jsp页面元素构成 jsp页面组成部分有:指令,注释,静态内容,表达式,小脚本,声明. jsp指令 page指令:通常位于jsp页面的顶端,同一个页面可以有多个page指令 include指令:将一个 ...

  4. C++专题 - Qt是什么

    Qt是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程式,也可用于开发非GUI程式,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊的代码生成扩展(称 ...

  5. WampServer修改MySQL密码

    WampServer安装后密码是空的,需要设置一下 一般有两种方式: 一是通过phpMyAdmin直接修改: 二是使用WAMP的MySql控制台修改. 第一种: ①在phpMyAdmin界面中点击[用 ...

  6. eclipse下使用Genymotion调试Android程序出现的问题

    一. The connection to adb is down, and a severe error has occured. You must restart adb and Eclipse. ...

  7. css字体大小设置em与rem的区别

    em 单位em是相对于父元素的,如果父元素没有设置字体大小,那就会追溯到body. 比如  如果我在box_text的父元素box加了一个字体大小   那么body的8px就会被box_text的父元 ...

  8. Convention插件与“约定”支持

    主要用于Action映射和Result映射 struts2-convention-plugin-2.3.16.3.jar 会将 |--实现了com.opensymphony.xwork2.Action ...

  9. Linux 多用户和多用户边界

    1. 需求背景 2. 多用户的边界: 独立的工作目录 3. 多用户的边界:可操作/访问的资源 4. 多用户的边界: 可执行的操作 5. 多用户的特性标识: UID和GID -------------- ...

  10. core文件找不到了

    开始以为是core文件太大,设置ulimit -c unlimited  以后,再次访问,显示 ./a.out Segmentation fault (core dumped) 但是却找不到这个文件的 ...