Reading BLOB data through oracledatareader
In actual application, large binary data must be stored in the database. To read these large data blocks, you can forcibly convert the data type into a byte array, but when the binary data body is large enough (dozens of megabytes or hundreds of megabytes ), the full length cannot be obtained at one time, so it must be obtained in multiple parts.
The following two methods are provided for your reference.
1. directly convert BLOB data into byte Arrays
Long Blobdatasize = 0 ; // Actual BLOB data size
Long Readstartbyte = 0 ; // Reads data from where the BLOB Data body starts.
Int Bufferstartbyte = 0 ; // Write Data from where the buffer array starts
Int Hopereadsize = 1024 ; // The size of the data that you want to read from the BLOB Data body each time.
Long Realreadsize = 0 ; // Size of the data actually read from the BLOB Data body each time
// Commandbehavior. sequentialaccess enables oracledatareader to load BLOB data as a stream
Oracledatareader Dr = Cmd. executereader (commandbehavior. sequentialaccess );
Byte [] Buffer = Null ;
While (Dr. Read ())
{
Blobdatasize = Dr. getbytes ( 0 , 0 , Null , 0 , 0 ); // Obtains the total size of the BLOB Data body.
Buffer = New Byte [Blobdatasize];
Realreadsize = Dr. getbytes ( 0 , Readstartbyte, buffer, bufferstartbyte, hopereadsize );
// Loop, read the 1024byte size each time
While (( Int ) Realreadsize = Hopereadsize)
{
Bufferstartbyte + = Hopereadsize;
Readstartbyte + = Realreadsize;
Realreadsize = Dr. getbytes ( 0 , Readstartbyte, buffer, bufferstartbyte, hopereadsize );
}
// Read the remaining data smaller than bytes at the end of the BLOB Data body.
Dr. getbytes ( 0 , Readstartbyte, buffer, bufferstartbyte ,( Int ) Realreadsize );
// After reading, the binary data of the BLOB Data body is converted to the byte array buffer.
}
2. directly write BLOB data to the file
Long Readstartbyte = 0 ; // Reads data from where the BLOB Data body starts.
Int Hopereadsize = 1024 ; // The size of the data that you want to read from the BLOB Data body each time.
Long Realreadsize = 0 ; // Size of the data actually read from the BLOB Data body each time
// Commandbehavior. sequentialaccess enables oracledatareader to load BLOB data as a stream
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 );
// Loop, read bytes each time, and write these bytes into the stream
While (( Int ) Realreadsize = Hopereadsize)
{
FS. Write (buffer, 0 , Hopereadsize );
Readstartbyte + = Realreadsize;
Realreadsize = Dr. getbytes ( 0 , Readstartbyte, buffer, 0 , Hopereadsize );
}
// Read the remaining data smaller than bytes at the end of the BLOB Data body and write these bytes into the stream.
Realreadsize = Dr. getbytes ( 0 , Readstartbyte, buffer, 0 , Hopereadsize );
FS. Write (buffer, 0 ,( Int ) Realreadsize );
}
Reprinted from: http://blog.csdn.net/lonet/archive/2010/03/03/5342386.aspx