I am using Windows Form and MySQL for my project. In that i want to save a image and retrieve that.
I have created a table named 'image' in that,
CREATE TABLE `image` (
`id` INT(15) NOT NULL AUTO_INCREMENT,
`extension` VARCHAR(50) NOT NULL,
`image` LONGBLOB NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=2
And
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
if (open.ShowDialog() == DialogResult.OK)
{
txt_imagePath.Text = open.FileName;
}
hp.getConnStr();
try
{
MySqlConnection connection = new MySqlConnection(hp.myConnStr);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "insert into image (image) values ('"+txt_imagePath.Text +"')";
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
in Browse button click event,..
The file is successfully saved. Now i want to retrieve that picture and show in picture box. So that i try this bellow code,..
MySqlConnection connection = new MySqlConnection(hp.myConnStr);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select image from image";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
pictureBox1.Image = new Bitmap(Reader[0].ToString());
}
connection.Close();
but no use.
Please help me.
I hope something like this would help:
this should do it. reader.GetValue() returns byte array on MySQL blobs, this does the trick.
You should read the blob field as a
MemoryStream
and set toImage
property of the control usingImage.FromStream()
.