Depending on the size of the image, I wouldn't recommend storing it in the DB directly - small thumbnail sizes are ok, but images these days get big pretty fast, and can take up a lot of SQL server bandwidth and DB space. If you are looking at storing a lot of largish images, I would create a folder for them, and store them under a temporary name, recording that in the DB instead. Personally, I store a thumb in the DB, with a path reference to the full image.
However, it isn't difficult to store or retrieve images:
using (SqlConnection con = new SqlConnection(GenericData.DBConnection))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO Images (Id, Location, ImageData, InsertDate) VALUES (@ID, @DS, @TN, @DI)", con))
{
cmd.Parameters.AddWithValue("@ID", Id);
cmd.Parameters.AddWithValue("@DS", Location);
cmd.Parameters.AddWithValue("@TN", myImage.ToByteArray());
cmd.Parameters.AddWithValue("@DI", InsertDate);
cmd.ExecuteNonQuery();
}
}
using (SqlConnection con = new SqlConnection(GenericData.DBConnection))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT ImageData FROM Images WHERE Id=@ID", con))
{
cmd.Parameters.AddWithValue("@ID", Id);
SqlDataReader r = cmd.ExecuteReader();
if (r.Read())
{
MemoryStream ms = new MemoryStream((byte[])r["ImageData"]);
MyImage = Image.FromStream(ms);
}
}
}