下面提供两种方法,供大家参考。
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())
{
blobDataSize = dr.GetBytes(0, 0, null, 0, 0); //获取这个BLOB数据体的总大小
buffer = new byte[blobDataSize];
realReadSize = dr.GetBytes(0, readStartByte, buffer, bufferStartByte, hopeReadSize);
//循环,每次读取1024byte大小
while ((int)realReadSize == hopeReadSize)
{
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上去了
}
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())
{
blobDataSize = dr.GetBytes(0, 0, null, 0, 0); //获取这个BLOB数据体的总大小
buffer = new byte[blobDataSize];
realReadSize = dr.GetBytes(0, readStartByte, buffer, bufferStartByte, hopeReadSize);
//循环,每次读取1024byte大小
while ((int)realReadSize == hopeReadSize)
{
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);
}