Here's the sample. It uses the database from my article Save and Retrieve Files from SQL Server Database using ASP.Net. Download the sample it has the database with image in it. And then use the following code
private void Form2_Load(object sender, EventArgs e)
{
using (MemoryStream mStream = new MemoryStream())
{
byte[] bytes = GetData();
mStream.Write(bytes, 0, Convert.ToInt32(bytes.Length));
pictureBox1.Image = Image.FromStream(mStream);
}
}
private byte[] GetData()
{
string conString = @"Data Source=.\SQL2005;Initial Catalog=dbFiles;User id = sa;password=pass@123";
string query = "select Name, ContentType, Data from tblFiles where id=@id";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@id", 3);
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return (byte[])dt.Rows[0]["Data"];
}
}
}
}