如何在 Visual C++/CLI 中从 MySQL 检索我的图像

How Can I retrieve my Image From MySQL in visual C++/CLI

我想使用 Visual C++ 从 MySQL 数据库中检索我的图像,其类型为 LONG BLOB 我在关键事件中尝试这样做

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();

我得到错误的问题

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();


第三次尝试索引超出数组范围请帮忙
这解决了我选择了错误的列

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();

我从 C# 代码中获取此代码,我尝试对其进行修改,以便我可以在 C++ 中使用它 我没有找到任何从 MySQL 在 Visual C++ 中检索图像的答案

注意:我使用的是 Visual C++ 2015

BinaryFormatter^ bf = gcnew BinaryFormatter(); // to convert object to byte array

该对象包含一个字节数组。将包含字节数组的对象转换为字节数组会产生不同于原始字节数组的结果。您可以通过创建一个字节数组、序列化它,然后检查内存流的内容(现在会更大)的简单示例来验证自己

我认为您需要在 reader 上使用 GetBytes()。您可以在 https://msdn.microsoft.com/en-us/library/87z0hy49%28v=vs.110%29.aspx and MySqlDataReader GetBytes buffer issue...

阅读更多内容

要解决此问题,您需要在文件顶部添加以下行:

using namespace System::IO;