图像保存到数据库 [英] Saving image to the database

查看:119
本文介绍了图像保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些简单的实体现在需要有一个配置文件的图像。什么是做到这一点的正确方法?因此,它是1对1的关系,一个图像仅涉及一个实体,反之亦然。此图片应该通过表单上载与插入相关实体在一起。

I have some simple entity which now needs to have a Profile image. What is the proper way to do this? So, it is 1 to 1 relationship, one image is related only to one entity and vice versa. This image should be uploaded through webform together with inserting related entity.

如果任何人都可以点我朝着正确的方向如何坚持图像数据库和相关的实体将是巨大的。

If anyone can point me to the right direction how to persist images to the db and related entity will be great.

推荐答案

只是一个侧面注释:我觉得是不是来存储图像是个好主意分贝

在一般不是以dB为单位一个好主意,存储图像作为DBS的设计存储文本没有大的二进制块。要好得多,以存储图像的路径和有一个文件夹中的图像。如果你想获得确保1对1的关系名形象与实体(1323.jpg)的ID。

In general is not a good idea store images in db as dbs are designed to store text not big binary chunks. Is much better to store paths for images and have images in a folder. If you want to get sure of 1 to 1 relationship name image with ID of entity (1323.jpg).

如果你想有图像路径应遵循的一些准则(一般code防守):

If you want to have image paths you should follow some guidelines (In general code defensively):


  • 在图像上传检查图像是有效的(甚至提出映像头的二进制校验)

  • 请不要让一个新的实体的INSERT的情况下,以覆盖现有的图像。

  • 名称图像作为主键(1.JPG,2.JPG)

  • 在图像的负载不要以为图像会在那里。

  • 请不要让(如果可能)与图像人工交互(在机将影像复制到其他没有远程从一个地方)。人工交互可能会导致不一致。

不过,我认为由于某种原因,你应该这样做。 因此,为了实现你想要的:

But I assume that for some reason you should do it. So in order to achieve what you want:

DB设计


  • 在您的表创建一个二进制列(binary或varbinary)

  • ,如果你在不同的表1-1关系创建更好。不过倒也避免了保湿实体时加载图像。使用延迟加载的方式来加载你的形象,只有当你想要的。

  • 您必须避免加载图像时,你犯了一个很大的选择(例如,如果要加载在组合所有实体避免SELECT * FROM无论),因为它会加载景象万千白白。正如我说,这可以通过在不同的表图像,或选择loading唯一正确的列或通过延迟加载来完成。 (甚至更好地没有在DB的图片,只有路径)

C#code


  • 使用BynaryReader读它

  • 用户字节数组来存储它

  • Use BynaryReader to read it
  • User Byte array to store it

检查此链接为code例如:http://www.$c$cproject.com/Articles/21208/Store-or-Save-images-in-SQL-Server

Check this link for code example: http://www.codeproject.com/Articles/21208/Store-or-Save-images-in-SQL-Server

这篇关于图像保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆