51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

如何正确地将图像存储到SQL Server

英文:

How to properly store an image to SQL Server

问题 {#heading}

我已经使用以下代码将一些数据存储在SQL Server数据库中:

_bitmap = BitmapFactory.decodeFile(path);
byteArrayOutputStream = new ByteArrayOutputStream();
_bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
bytesImage = byteArrayOutputStream.toByteArray();
image = Base64.encodeToString(bytesImage, Base64.DEFAULT);

String query =
    "INSERT INTO EloanApp(SeqID,AppImage) VALUES ('" + seqid + "','" + image + "')";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);

为了确保成功上传图像,我尝试导出存储在我的数据库中的图像并打开它。照片查看器显示**"看起来我们不支持这个文件格式。"**。这里可能出现了什么问题?

PS.

我使用以下方法导出了图像:

Simple Image Import and Export Using T-SQL for SQL Server 英文:

I have already stored some data in the SQL Server database with this code

_bitmap = BitmapFactory.decodeFile(path);
byteArrayOutputStream = new ByteArrayOutputStream();
_bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
bytesImage = byteArrayOutputStream.toByteArray();
image = Base64.encodeToString(bytesImage, Base64.DEFAULT);
`String query =
"INSERT INTO EloanApp(SeqID,AppImage) VALUES ('" + seqid + "','" + image + "')";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
`

And to make it sure that if it is successfully uploaded the image, I tried to export the image stored in my database and opened it, the photo viewer, shows "It looks like we don't support this file format." what seems could be the problem here?

PS.

I exported the image with this method

Simple Image Import and Export Using T-SQL for SQL Server

答案1 {#1}

得分: 0

事实证明,移除这一行代码:

image = Base64.encodeToString(bytesImage, Base64.DEFAULT);

并且使用 PreparedStatement 来执行我的查询:

PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO EloanApp(SeqID,AppImage) VALUES(?,?);
`preparedStatement.setString(1, seq_id);
preparedStatement.setBytes(2, bytesImage);
preparedStatement.execute();
`

问题已解决! 英文:

It turned out that removing this line

image = Base64.encodeToString(bytesImage, Base64.DEFAULT);

and using a PreparedStatement to execute my query

PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO EloanApp(SeqID,AppImage) VALUES(?,?);
`preparedStatement.setString(1, seq_id);
preparedStatement.setBytes(2, bytesImage);
preparedStatement.execute();
`

solved the issue!


赞(1)
未经允许不得转载:工具盒子 » 如何正确地将图像存储到SQL Server