【发布时间】:2017-01-28 00:12:14
【问题描述】:
我正在开发一个 VC++ cli 应用程序,我需要将图像存储到 MySql 数据库中。我意识到这通常不是一个好的做法,但由于对文件系统的访问受限,这是我必须走的路。
所以我已经能够将图像放入一个托管的 unsigned char 数组中,这看起来就像我在网上找到的示例中必须做的(这些示例大多在 C# 中并且包含不可用的命令)。我只需要弄清楚如何将其输入数据库,我不知所措,而且我的搜索至少没有发现任何对 VC++ 有用的东西。
这是我需要做的,我有一个托管数组:
array<unsigned char>^ imageSource;
包含图像的字节,使用:
System::IO::FileStream^ fs;
System::IO::BinaryReader^ br;
//Read the Image
fs = gcnew System::IO::FileStream(filepath, System::IO::FileMode::Open, System::IO::FileAccess::Read);
br = gcnew System::IO::BinaryReader(fs);
//Store the Image into an array
imageSource = br->ReadBytes((int)fs->Length);
接下来我需要将它保存到我的数据库中:
sql::Connection *sqlConn;
sql::PreparedStatement *sqlStmt;
sql::ResultSet *sqlResult;
// Sql Connection Code
//I have a Routine to save images
//and have Select because get_last_insert_id() is not available in the c++ connector
sqlStr = "SELECT save_item_image (?, ?, ?, ?, ?) AS ID";
//Prepare the Query
sqlStmt = this->sqlConn->prepareStatement(sqlStr);
//Set Parameters in SqlStatment
sqlStmt->setInt(1, 1);
sqlStmt->setInt(2, 1);
sqlStmt->setBlob(3, &imgSource); <-- This is where I need to insert image
sqlStmt->setString(4, "jpg");
sqlStmt->setString(5, "test.jpg");
sqlStmt->executeUpdate();
据我了解,setBlob 函数需要 std::istream 来读取数据。我不知道该怎么做,这是我的主要问题。
【问题讨论】:
-
嗯我还是不明白为什么你选择而不是插入...
-
@KonstantinosKamaropoulos 这是一个插入函数,我必须使用 SELECT 才能获取插入的 id,因为 C++ 连接器不像其他语言那样具有 last_insert_id()。跨度>
-
哦,所以你正在插入并获取你的行得到的 id?
-
@KonstantinosKamaropoulos 是的。我几乎将托管数组放入 istream 中,这就是问题所在。
-
为什么首先要使用无符号字符数组?
标签: mysql image visual-c++ c++-cli