Welcome to Stackoverflow. I see that you want to get an image from the database. First, check if the image is inserted correctly. A right way to do it is to convert the image to a byte[] array than send it as a parameter. This is how I did it:
static public void UpdateProfilePicture(int id,Image img)
{
using (MySqlConnection conn = new MySqlConnection(ConnectionString.connectionString))
{
conn.Open();
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.CommandText = "UPDATE Users SET UserProfilePicture = @img WHERE idUser = @id";
cmd.Connection = conn;
var userImage = ImageToByte(img);
var paramUserImage = new MySqlParameter("@img", MySqlDbType.Blob, userImage.Length);
paramUserImage.Value = userImage;
cmd.Parameters.Add(paramUserImage);
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
}
}
}
private static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
If you manage your mysql database with MySqlWorbench you can click on the cell where you inserted the image and it will appear in a window in a byte array format. Also, you can see the whole image if you go through the upper tabs of the window(I think this method also works in PhpMyAdmin)(Or you can search the internet for a byte array to image converter website).
After that, if everything is fine, here is a code sample that will help you retrieve the image:
static public Image GetUserImageById(int id)
{
MySqlDataAdapter da;
using (MySqlConnection conn = new MySqlConnection(ConnectionString.connectionString))
{
conn.Open();
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.CommandText = "SELECT UserProfilePicture FROM Users WHERE idUser = @id";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@id", id);
da = new MySqlDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);
byte[] img = (byte[])table.Rows[0][0];
MemoryStream ms = new MemoryStream(img);
Image image;
if (ms.Length > 0) image = Image.FromStream(ms);
else
image = null;
da.Dispose();
return image;
}
}
}
P.S: If the format of the image or the file you want to insert in the database is not in a byte array format, than you won`t be able to retreive it.