If you are using Windows Forms, its pretty easy.
For storing:
conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
conn.Open();
FileStream fs;
Byte[] bindata;
MySqlParameter picpara;
cmd = new MySqlCommand("INSERT INTO mypic (pic) VALUES(?pic)", conn);
picpara = cmd.Parameters.Add("?pic", MySqlDbType.MediumBlob);
cmd.Prepare();
fs = new FileStream(txtPicPath.Text, FileMode.Open, FileAccess.Read);
bindata = new byte[Convert.ToInt32(fs.Length)];
fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
fs.Close();
picpara.Value = bindata;
cmd.ExecuteNonQuery();
To retrieve it:
if (conn == null)
{
conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
conn.Open();
}
MemoryStream ms = new MemoryStream();
FileStream fs;
Byte[] bindata;
cmd = new MySqlCommand("SELECT pic FROM mypic WHERE id=3", conn);
bindata = (byte[])(cmd.ExecuteScalar());
ms.Write(bindata, 0, bindata.Length);
pb2.Image = new Bitmap(ms);
fs = new FileStream(name, FileMode.Create, FileAccess.Write);
ms.WriteTo(fs);
Notes:
pb1 is a picture box that allows me to see the preview and
pb2 is where the retrieved image is shown.
The database name is
databaseimage
The table name is
mypic.
The table has two columns,
pic (type is
mediumblob) and
id (type is
int and it is
autoincreament). The id column allows one to select different pictures in the db, e.g. for my case i am getting row 3:
cmd = new MySqlCommand("SELECT pic FROM mypic WHERE id=3", conn);