Reading BLOB data through oracledatareader

Source: Internet
Author: User
 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.