您的位置:首页 > 数据库 > Oracle

通过OracleDataReader来读取BLOB类型的数据 (转载)

2010-10-25 20:45 796 查看

通过OracleDataReader来读取BLOB类型的数据

document.body.oncopy = function () {
if (window.clipboardData) {
setTimeout(function () {
var text = clipboardData.getData("text");
if (text && text.length > 300) {
text = text + "\r\n\n本文来自CSDN博客,转载请标明出处:" + location.href;
clipboardData.setData("text", text);
}
}, 100);
}
}
function StorePage() { d = document; t = d.selection ? (d.selection.type != 'None' ? d.selection.createRange().text : '') : (d.getSelection ? d.getSelection() : ''); void (keyit = window.open('http://www.365key.com/storeit.aspx?t=' + escape(d.title) + '&u=' + escape(d.location.href) + '&c=' + escape(t), 'keyit', 'scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus(); }
在实际的应用过程中,需要把大块的二进制数据存储在数据库中。读取这些大块的数据,可以通过强制类型转换成为byte数组,但是当这个二进制数据体够大时(几十兆或者上百兆),一次并不能获取到他的完整长度,所以需要分块获取。
下面提供两种方法,供大家参考。
1、直接将BLOB数据转换成为byte数组

long blobDataSize = 0; //BLOB数据体实际大小
long readStartByte = 0;//从BLOB数据体的何处开始读取数据
int bufferStartByte = 0;//将数据从buffer数组的何处开始写入
int hopeReadSize = 1024; //希望每次从BLOB数据体中读取数据的大小
long realReadSize = 0;//每次实际从BLOB数据体中读取数据的大小
//CommandBehavior.SequentialAccess将使OracleDataReader以流的方式加载BLOB数据
OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
byte[] buffer = null;
while (dr.Read())
{
{
bufferStartByte += hopeReadSize;
readStartByte += realReadSize;
realReadSize= dr.GetBytes(0, readStartByte, buffer, bufferStartByte, hopeReadSize);
}
//读取BLOB数据体最后剩余的小于1024byte大小的数据
dr.GetBytes(0, readStartByte, buffer, bufferStartByte, (int)realReadSize);
//读取完成后,BLOB数据体的二进制数据就转换到这个byte数组buffer上去了
}

2、将BLOB数据直接写到文件中去

long readStartByte = 0;//从BLOB数据体的何处开始读取数据
int hopeReadSize = 1024; //希望每次从BLOB数据体中读取数据的大小
long realReadSize = 0;//每次实际从BLOB数据体中读取数据的大小
//CommandBehavior.SequentialAccess将使OracleDataReader以流的方式加载BLOB数据
OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while (dr.Read())
{
FileStream fs = new FileStream(filename, FileMode.Create);
byte[] buffer = new byte[hopeReadSize];
realReadSize = dr.GetBytes(0, readStartByte, buffer, 0, hopeReadSize);
//循环,每次读取1024byte大小,并将这些字节写入流中
while ((int)realReadSize == hopeReadSize)
{
fs.Write(buffer, 0, hopeReadSize);
readStartByte += realReadSize;
realReadSize= dr.GetBytes(0, readStartByte, buffer, 0, hopeReadSize);
}
//读取BLOB数据体最后剩余的小于1024byte大小的数据,并将这些字节写入流中
realReadSize= dr.GetBytes(0, readStartByte, buffer, 0, hopeReadSize);
fs.Write(buffer, 0, (int)realReadSize);
}

转载自:http://blog.csdn.net/lonet/archive/2010/03/03/5342386.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: