将图像存储在 SQL Server CE 数据库中
有谁知道如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是一篇 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.
为什么不使用 Image 类型? 这里是类型的描述,支持SQL Server CE。
Why aren't you using the Image type? Here is a description of types, supported by the sql server ce.
这不是对您问题的直接答案,但我已经成功地将图像的文件路径(例如: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.