mysql - 如何在 Visual C++/CLI 中从 MySQL 检索图像

标签 mysql visual-c++ 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++ 中使用它,但我没有找到任何在 Visual C++ 中从 MySQL 检索图像的答案

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

最佳答案

BinaryFormatter^ bf = gcnew BinaryFormatter();//将对象转换为字节数组

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

我认为您需要在阅读器上使用 GetBytes() 。您可以阅读https://msdn.microsoft.com/en-us/library/87z0hy49%28v=vs.110%29.aspx了解更多信息和 MySqlDataReader GetBytes buffer issue...

关于mysql - 如何在 Visual C++/CLI 中从 MySQL 检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083359/

相关文章:

c++ - 关于匿名命名空间和内部链接的标准是什么?

mfc - 如何默认激活 "disable display scaling on high dpi settings"

.net - 如何更改C++/CLI winform项目中的应用程序图标?

c++ - 如何让 MSVC 将未初始化的数据放入 .bss?

c++ - 如何在 C++ 中使用 fstream 读取和写入无符号字符到文件?

c++ - C++ 和 C++ CLI 有什么区别

MySql sql递归循环

mysql - mysql 表 (MyISAM) 中的索引如何工作?

mysql - 带条件的关系选择

mysql - 从多对多关系中获取数据