如何从visual studio中的访问数据库中检索图像? [英] How do I retrieve an image from an access database in visual studio?

查看:139
本文介绍了如何从visual studio中的访问数据库中检索图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将存储在Access数据库中的图像检索到Picturebox。我已成功设法使用OLE对象将图像存储到数据库中,我对这种语言不是很流利,但我目前的代码如下:



< b>我尝试了什么:



I'm trying to retrieve an image stored in an Access Database to a Picturebox. I have successfully managed to store the image into the database using OLE Object, I'm not very fluent with this language, but my current code is as follows:

What I have tried:

Private Sub lstUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsers.SelectedIndexChanged

    Dim dt As New DataTable

    dt = runSQL("Select * from tblUser where UserName = '" & lstUsers.SelectedItem.ToString & "'")

    txtForename.Text = dt.Rows(0)(2)
    txtSurname.Text = dt.Rows(0)(3)
    txtCode.Text = dt.Rows(0)(6)
    txtFinish.Text = dt.Rows(0)(7)
    PictureBox1.Image = dt.Rows(0)(8)

End Sub

< br $>




当我尝试运行此选项并选择用户名时,我收到以下错误消息:






When I try to run this and I select a username I get the following error message:

Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.





任何帮助都将不胜感激。



Any help would be appreciated.

推荐答案

您不能直接将字节数组分配给图像对象。首先你将字节数组转换为图像对象,然后就可以了。

以下是解决方案:C#vresion

You can not directly assign byte array to image object. First you have convert byte array to image object and then you can.
following is the solution: C# vresion
byte[] data = (byte[]) dt.Rows(0)(8);
using (MemoryStream ms = new MemoryStream(data ))
{
pictureBox1.Image = Image.FromStream(ms);
}





Vb.Net版本:



Vb.Net version:

Dim data As Byte() = DirectCast(dt.Rows(0)(8), Byte())
Using ms As New MemoryStream(data)
	pictureBox1.Image = Image.FromStream(ms)
End Using



问候,

Imdadhusen


Regards,
Imdadhusen


你不能直接将二进制图像数据放在图片框中,你首先需要从数据中创建图像对象。



有一篇很好的文章。请参阅:使用SQL Server数据库保存和检索图像VB.NET [ ^ ]



如果数据库是SQL Server,请不要担心,图像的想法是一样的。
You cannot place the binary image data directly picturebox, you first need to create the image object from the data.

There's a nice article about this. See: Save and Retrieve Image from a SQL Server Database using VB.NET[^]

Don't worry if the database is SQL Server, the idea for the image is the same.


这里有很多问题:首先,永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。

其次,永远不要使用 SELECT * FROM - 始终准确指定要检索的列,以及您的顺序要他们。这样,您就不会传输不需要的数据 - 这样可以节省带宽 - 并且您的代码对数据库的更改更具免疫力,从而导致崩溃,因为列顺序不同,或者数据损坏更严重。如果您使用数字索引来检索列内容而不是基于字符串的列名称,这一点尤为重要。

第三,不要使用DataTable来检索单个用户详细信息 - 请改用DataReader,它是一个更高效的内存 - 并在您尝试访问之前检查返回以确保有数据!如果没有具有该名称的用户,则代码崩溃并烧毁...

第四,不要使用解决方案1中的代码:Image.FromStream方法(流)(System.Drawing) [ ^ ]非常具体,流必须保持打开状态图像的生命周期,使用块不允许。

There are a number of problems here: first off never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Second, never use SELECT * FROM - always specify exactly which columns you want to retrieve, and the order in which you want them. That way, you don't transfer data you don't need - which saves bandwidth - and your code becomes more immune to changes on the database causing it to crash because the column order is different, or worse corrupt data. This is particularly important if you use numeric indexes to retrieve column content instead of string based column names.
Thirdly, don't use a DataTable to retrieve a single user details - use a DataReader instead, it's a lot more memory efficient - and check the return to make sure there is data before you try to access it! If there is no user with that name, your code crashes and burns...
Fourthly, do not use the code in Solution 1: the documentation for Image.FromStream Method (Stream) (System.Drawing)[^] is very specific that the stream must remain open for the life of the image, which the using block does not allow.
Dim ms As New MemoryStream(bytes)
Dim useThisImage As Image = Image.FromStream(ms)


这篇关于如何从visual studio中的访问数据库中检索图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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