将图像存储在 SQL Server CE 数据库中

发布于 2024-08-28 20:01:29 字数 758 浏览 3 评论 0原文

有谁知道如何在 SQL Server CE 数据库中存储图像的示例吗?

该列应该是什么数据类型? (我猜是二进制的。)

我使用 Linq-To-Datasets。是否可以使用它将图像放入数据库并稍后再将其取出?

感谢您的任何建议。


我是这样做的:

MemoryStream stream = new MemoryStream();
myBitmapImage.Save(stream, ImageFormat.Png);
myInsertLinqToDataSetRow.IMAGE_COLUMN = stream.ToArray();

为了再次加载它,我这样做了:

MemoryStream stream = new MemoryStream(myLinqToDataSetRow.IMAGE_COLUMN);
myBitmapImage.SignatureImage = new Bitmap(stream);

我在 MSDN 上找到了一个页面,上面说 Image 列类型即将消失,您应该使用 varbinary(MAX)。 SQL Server CE 不支持 Max,因此我使用了 varbinary(8000)。

稍后注意:虽然 SQL Server CE 不支持 varbinary(max)。 Varbinary(8000) 对于许多图像来说不够大。我最终还是使用了 Image 类型,尽管它计划被弃用。一旦微软在移动平台上提供了合理的替代方案,我就会考虑更换。

Does any one know of an example on how to store an image in a SQL Server CE database?

What data type should the column be? (I am guessing binary.)

I use Linq-To-Datasets. Is it possible using that to put the image into the database and pull it out again later?

Thanks for any advice.


Here is how I did it:

MemoryStream stream = new MemoryStream();
myBitmapImage.Save(stream, ImageFormat.Png);
myInsertLinqToDataSetRow.IMAGE_COLUMN = stream.ToArray();

To load it back out again I did this:

MemoryStream stream = new MemoryStream(myLinqToDataSetRow.IMAGE_COLUMN);
myBitmapImage.SignatureImage = new Bitmap(stream);

I found a page on MSDN that said that the Image column type is going away and that you should use varbinary(MAX). Max is not supported on SQL Server CE so I did varbinary(8000).

LATER NOTE: while varbinary(max) is not supported on SQL Server CE. Varbinary(8000) is not big enough for many images. I did end up using the Image type even though it is planned to be deprecated. Once ms offers a resonable alternitive on the mobile platform I will consider switching.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄暮涼年 2024-09-04 20:01:29

以下是一篇 MSDN 文章,解释了如何操作:

http://support.microsoft.com/kb/318639

不幸的是,该示例是用 VB 编写的,但我相信您可以了解如何做到这一点。

我想说二进制数据类型非常适合存储图像。

Here is an MSDN article explaining how:

http://support.microsoft.com/kb/318639

Unfortunately, the example is in VB, but I am sure that you can get the idea of how to do it.

I would say the binary datatype would be fine for storing the images.

秋意浓 2024-09-04 20:01:29

为什么不使用 Image 类型? 这里是类型的描述,支持SQL Server CE。

图像 - 可变长度二进制数据,最大长度为 2^30 – 1 (1,073,741,823) 字节。

Why aren't you using the Image type? Here is a description of types, supported by the sql server ce.

Image - Variable-length binary data with a maximum length of 2^30 – 1 (1,073,741,823) bytes.

花辞树 2024-09-04 20:01:29

这不是对您问题的直接答案,但我已经成功地将图像的文件路径(例如:C:\images\image1.png)作为字符串值而不是原始图像存储在数据库中。

这样做的一个优点是可以减小数据库的大小。

另一个是多个表可以指向图像,而无需存储图像的多个副本。

This is not a direct answer to your question, but I've had good success storing the image's filepath (example: C:\images\image1.png) as a string value in the database instead of the raw image.

One advantage to this is that it keeps the database size smaller.

Another is that multiple tables can point to the images without storing multiple copies of the image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文