I want to retrieve my image from MySQL database the type of it LONG BLOB using visual C++ I try this in my key event
String^ con = L"datasource=localhost;port=3306;username=root;password=root";
MySqlConnection^ conn = gcnew MySqlConnection(con);
MySqlCommand^ cmd = gcnew MySqlCommand("select * from test.testphoto where test.testphoto.name=@name",conn);
MySqlDataReader^ re;
try {
conn->Open();
cmd->Parameters->AddWithValue("@name", "shakira");
re = cmd->ExecuteReader();
while (re->Read()) {
BinaryFormatter^ bf = gcnew BinaryFormatter(); // to convert object to byte array
MemoryStream^ ms = gcnew MemoryStream();
bf->Serialize(ms, re->GetValue(1));
pictureBox1->Image = gcnew Bitmap(ms);
MessageBox::Show("Excute");
}
}
catch (Exception ^e) {
MessageBox::Show(e->Message, "Error");
}
conn->Close();
The problem here that I get error
parameter is not valid
Update
i try this now and same Error parameter is not valid please any help
String^ con = L"datasource=localhost;port=3306;username=root;password=kapookingkong";
MySqlConnection^ conn = gcnew MySqlConnection(con);
MySqlCommand^ cmd = gcnew MySqlCommand("select * from test.testphoto where test.testphoto.name='shakira'",conn);
MySqlDataReader^ re;
try {
conn->Open();
re = cmd->ExecuteReader();
re->Read();
BinaryFormatter^ fe = gcnew BinaryFormatter();
MemoryStream^ ms = gcnew MemoryStream();
fe->Serialize(ms, re["photo"]);
array<Byte>^ arr = ms->ToArray();
MemoryStream^ ms2 = gcnew MemoryStream(arr);
pictureBox1->Image = Image::FromStream(ms2);
}
catch (Exception ^e) {
MessageBox::Show(e->Message, "Error");
}
conn->Close();
third try this time index outside bounds of the array Any Help please
this solved I choose the wrong column
String^ con = L"datasource=localhost;port=3306;username=root;password=root";
MySqlConnection^ conn = gcnew MySqlConnection(con);
MySqlCommand^ cmd = gcnew MySqlCommand("select photo from test.testphoto where test.testphoto.name='amr'",conn);
MySqlDataReader^ re;
try {
conn->Open();
re = cmd->ExecuteReader();
array<Byte>^ arr;
while (re->Read())
{
long long l = re->GetBytes(1, 0, nullptr, 0, 0);
arr = gcnew array<Byte>(l);
re->GetBytes(1, 0, arr, 0, l);
}
pictureBox1->Image = Image::FromStream(gcnew MemoryStream(arr));
pictureBox1->Refresh();
}
catch (Exception ^e) {
MessageBox::Show(e->Message, "Error");
}
conn->Close();
I get this code from C# code, I try to modify it so I can use it in C++ I didn't find any answer to retrieve image from MySQL in visual C++
Note: I'm using visual C++ 2015