Say I have stored images in a field called Picture, in a database called productcatalog, and from table Products, I want to retrieve those images and put them in a PictureBox in a Winform.
I tried with below code, but it is not able to work. It says "Unable to connect to any of the specified MySQL hosts". I have used the same connection string in other application, and it is able to retrieve some other data from the same database and same table.
Any workable demo code would be greatly appreciated.
private void GetImage_button_Click(object sender, EventArgs e)
{
const string connStr = "server=localhost;database=productcatalog;uid=root;password=12345678";
const string query = "SELECT Picture FROM Products WHERE ProductID = 1004";
using (MySqlConnection conn = new MySqlConnection(connStr))
{
conn.Open();
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
using (MySqlDataReader dr = cmd.ExecuteReader())
{
using (DataTable dt = new DataTable())
{
dt.Load(dr);
using (MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["Picture"]))
{
ms.Position = 0;
ms.Seek(0, SeekOrigin.Begin);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
ms.Flush();
ms.Close();
}
}
}
}
}
}

1 answers
sorry, my above code works perfectly now.
answered 2 years ago by:
494