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