473,769 Members | 3,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

save BLOB to file from MySQL using c#

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(retur ns string:( )
My code:
--
DataTable dt = new MySQLSelectComm and(...; //select that row and column
where is BLOB
string dest = Server.MapPath( "image.jpg" );
FileStream binFile = new
FileStream(dest ,FileMode.OpenO rCreate,FileAcc ess.Write);
--
Now I want to use - binFile.Write(b uf,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(objec t o)
{
MemoryStream s = new MemoryStream();
BinaryFormatter b = new BinaryFormatter ();
b.Serialize(s, o);
if (s.Length > int.MaxValue)
throw new ArgumentExcepti on("Serialized object is larger than can fit into
byte array");
byte[] buffer = new Byte[s.Length];
s.Seek(0, SeekOrigin.Begi n);
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.
Nov 18 '05 #1
1 8713
I did it with ByteFX drivers, because MySQLDriverCS has not defined
methods(like MySQLDataReader .GetBytes(...) )

Here is sample code-
--
string connectionStrin g =

"Server=localho st;" +

"Database=iso_d okumentacia;" +

"User ID=;" +

"Password=; ";

MySqlConnection cn = new MySqlConnection (connectionStri ng);
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("S ELECT KapitolaBLOB FROM kapitola
WHERE ID_KAPITOLA=15" ,cn);
MySqlDataReader myReader =
logoCMD.Execute Reader(CommandB ehavior.Sequent ialAccess);

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.OpenOr Create, FileAccess.Writ e);

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.GetByt es(0, startIndex, outbyte, 0, bufferSize);

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

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

while (retval == bufferSize)

{

bw.Write(outbyt e);

bw.Flush();

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

startIndex += bufferSize;

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

}

// Write the remaining buffer.

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

bw.Flush();

// Close the output file.

bw.Close();

fs.Close();

}

myReader.Close( );

cn.Close();

}

--
Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7100
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and disadvantages of using a MySQL blob field rather than reading the images directly from the file? How does one insert an image into a blob field? Can it be done dynamically? Thank you John
3
3145
by: NotGiven | last post by:
I am researching the best place to put pictures. I have heard form both sides and I'd like to know why one is better than the other. Many thanks!
0
2326
by: Mark Fisher | last post by:
I cannot seem to store binary data as a BLOB. I used the following to create a table capable of holding a BLOB: CREATE TABLE blob_table ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, binary_stuff BLOB, PRIMARY KEY (id) );
7
11484
by: sime | last post by:
Hi, I have a blob field in a mysql database table. I want to copy a blob from one record to another. I am having trouble transferring the data via a php variable. Maybe I need to addslashes or convert to Hex or something. I've tried a few things but can't quite get it. Here is simplified code. mysql_select_db($dbname, $connection);
3
4726
by: hamvil79 | last post by:
I'm implementig a java web application using MySQL as database. The main function of the application is basically to redistribuite documents. Those documents (PDF, DOC with an average size around 2Mb) are stored in BLOB column. The amount of documents for the first year should not exceed 5/6 Giga, but I cannot make prevision for the next years. Those documents are mainly just accessed (update and delete are not so
0
2413
by: Big George | last post by:
Hello, I'm trying to save a jpg file of 300KB as a BLOB field in an Oracle 10g Database. If I try to call a Stored Procedure, it fails. If I use CommandText with SQL sentence, it success. I can't save the jpg file as a BLOB field using this Method (I'm calling a Stored Procedure): Private Sub save_BLOB(ByVal Photo As Byte())
2
6534
by: Vinciz | last post by:
hi guys... im new in java and i would love to learn some of these... basically i got a sample code to retrieve the blob from the mysql. however, i dont really know what to do with these retrieved byte/binary data as i got no idea on how to save them in our pc. For this situation, what i need to do is give the byte/binary data an extension (retrieved from another field in the table) in order to revert back to the original data i had in the...
4
5491
by: kev | last post by:
Hi folks, I have created a database to store information on equipments. During the first level of registration, there is a form that i need the user to fill up details on the equipment testing. i have done this one.Now what i need is to enable the users to upload files and save it into the corresponding table. Example: 3. A laser inventory form has been completed for each 3b or 4 laser and submitted to the Laser Safety Officer...
0
2555
by: hnpatel | last post by:
hello friends, i m making application in vb.NET. i m using MY SQL 5.0 and vs 2005 for this application. Now I want to save image file in sql database. i m using blob datatype of image field. i want to save image file from vb.net form. i m getting image in picturecontrol through openfiledialogbox. now how to save this image in my database through insert query? Plz help me for this..... Thanx in advance.....
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.