Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > save BLOB to file from MySQL using c#

Reply
Thread Tools

save BLOB to file from MySQL using c#

 
 
Markusek Peter
Guest
Posts: n/a
 
      02-19-2004
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.W rite);
--
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.


 
Reply With Quote
 
 
 
 
Markusek Peter
Guest
Posts: n/a
 
      02-19-2004
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.SequentialAc cess);

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();

}

--


 
Reply With Quote
 
 
 
 
chemacecilia chemacecilia is offline
Junior Member
Join Date: Jun 2007
Posts: 1
 
      06-09-2007
Hello. I found this message looking for a possible solution for exactly the same problem that you showed up long time ago. So, please if you could solved and you can help me now. I would be grateful.
Cheers
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Save xls file as blob to db ? tomo Java 2 01-29-2010 02:22 PM
How to save Ruby object as a blob in MySQL Bryan Richardson Ruby 3 09-05-2008 06:55 PM
BLOB - classic ASP using MySql Pasha Aryana ASP General 2 12-29-2007 12:10 PM
Blob using MySQL Moerderin Java 6 07-31-2004 01:07 PM
Re: Hibernate + MySQl - blob mapping question Roedy Green Java 0 08-23-2003 04:58 AM



Advertisments