Bug #5275 Reading LongBlob from the database provides corrupted data
Submitted: 28 Aug 2004 1:33 Modified: 16 Nov 2004 15:25
Reporter: Wayne Dickinson Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:0.7.6.15073 OS:Windows (Windows 2000)
Assigned to: Reggie Burnett CPU Architecture:Any

[28 Aug 2004 1:33] Wayne Dickinson
Description:
Hi there 

I have been having a problem reading and recreating a file that I saved to the database. The field is defined as a LongBlob. Now I have attached the code that I use to read the data from the database.

There are a couple of issues with this code:
- first is the fact that I need to specify the number of bytes to read
  as fileSize -1 (which is one byte shorter than the length of the data)
- second this code appears to work fine when I am retrieving a jpg or a gif
  file but ... (and here's the kicker) it doesn't work when you try and retrieve a PDF file stores in the database. If you're familiar with the file layout of a pdf file, the contents of the file begin with a first line that appears something like this "%PDF-1.3" minus the double quotes.
When I read in the pdf file from the database (via command line SQL) I confirm that the data is complete in the database. Also in the _fields private member variable in the data reader I see all of the data (plus some extra initial information)

For now I have managed to switch to use OdbcDataReader (and all the other Odbc classes) and the file retrieves just fine and I'm able to save it out to disk. Also using the Odbc reader the GetBytes command looks like this 
	dataReader.GetBytes(1, 0, buffer, 0, fileSize);

I am also curious about when you plan on releasing Connector/Net as a non-beta product

Thanks
Wayne Dickinson

How to repeat:
myConnection.Open();
MySqlDataReader dataReader;
int fileSize;
byte[] buffer;

command.CommandText = "Select length(fileContents) contentLength, fileContents from ReferenceFiles where fileid = 6";

dataReader = command.ExecuteReader();

FileStream fs = new FileStream("F:\\filename", System.IO.FileMode.CreateNew);

BinaryWriter bw = new BinaryWriter(fs);

while (dataReader.Read())
{
	fileSize = dataReader.GetInt32(0);
	buffer = new Byte[fileSize];
	dataReader.GetBytes(1, 0, buffer, 0, fileSize-1);
	bw.Write(buffer, 0, fileSize);
}
fs.Close();
myConnection.Close();

Suggested fix:
I've downloaded the latest code and I'll let you know if I come up with anything
[16 Nov 2004 15:25] Reggie Burnett
Thank you for taking the time to report a problem.  Unfortunately
you are not using a current version of the product your reported a
problem with -- the problem might already be fixed. Please download
a new version from http://www.mysql.com/downloads/

If you are able to reproduce the bug with one of the latest versions,
please change the version on this bug report to the version you
tested and change the status back to "Open".  Again, thank you for
your continued support of MySQL.