gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:02:35 25 4
gpt4 key购买 nike

我想使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com