save BLOB to file from MySQL using c#

M

Markusek Peter

Hi,
I'm using MySQLDriverCS. I've got no problem to store BLOB into database,
but I can't get it back(save to file).
Problem is with DataTable(returns string:( )
My code:
--
DataTable dt = new MySQLSelectCommand(...; //select that row and column
where is BLOB
string dest = Server.MapPath("image.jpg");
FileStream binFile = new
FileStream(dest,FileMode.OpenOrCreate,FileAccess.Write);
--
Now I want to use - binFile.Write(buf,0,buf.Length);. But problem is that
dt.Rows[0]["ColumnBLOB"] is System.String type, and I need byte[](for
binFile.Write)
When I use this function :
--
public byte[] Serialize(object o)
{
MemoryStream s = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(s, o);
if (s.Length > int.MaxValue)
throw new ArgumentException("Serialized object is larger than can fit into
byte array");
byte[] buffer = new Byte[s.Length];
s.Seek(0, SeekOrigin.Begin);
s.Read(buffer, 0, (int)s.Length);
s.Close();
return buffer;
}
--
BLOB is saved into the file with the right size, but most of the characters
are '?'(because
dt.Rows[0]["ColumnBLOB"] is string?)

=====================
I'm sorry for bad english.
Sample code will be appreciated:)
Thanks.
 
M

Markusek Peter

I did it with ByteFX drivers, because MySQLDriverCS has not defined
methods(like MySQLDataReader.GetBytes(...) )

Here is sample code-
--
string connectionString =

"Server=localhost;" +

"Database=iso_dokumentacia;" +

"User ID=;" +

"Password=;";

MySqlConnection cn = new MySqlConnection(connectionString);


FileStream fs; // Writes the BLOB to a file (*.jpg).

BinaryWriter bw; // Streams the BLOB to the FileStream object.

int bufferSize = 100; // Size of the BLOB buffer.

byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be
filled by GetBytes.

long retval; // The bytes returned from GetBytes.

long startIndex = 0; // The starting position in the BLOB output.


cn.Open();

MySqlCommand logoCMD = new MySqlCommand("SELECT KapitolaBLOB FROM kapitola
WHERE ID_KAPITOLA=15",cn);


MySqlDataReader myReader =
logoCMD.ExecuteReader(CommandBehavior.SequentialAccess);

while (myReader.Read())

{

// Get the publisher id, which must occur before getting the logo.


// Create a file to hold the output.

string dest = Server.MapPath("s.jpg");

fs = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.Write);

bw = new BinaryWriter(fs);

// Reset the starting byte for the new BLOB.

startIndex = 0;

// Read the bytes into outbyte[] and retain the number of bytes returned.

//myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);

retval =(long) myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);

// Continue reading and writing while there are bytes beyond the size of the
buffer.

while (retval == bufferSize)

{

bw.Write(outbyte);

bw.Flush();

// Reposition the start index to the end of the last buffer and fill the
buffer.

startIndex += bufferSize;

retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);

}

// Write the remaining buffer.

bw.Write(outbyte, 0, (int)retval - 1);

bw.Flush();

// Close the output file.

bw.Close();

fs.Close();

}

myReader.Close();

cn.Close();

}

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top