“参数无效"; VB.NET中的错误从MS Access数据库中将图像检索到PictureBox时 [英] "Parameter Not valid" Error in VB.NET While retrive image to picturebox from ms access database

查看:137
本文介绍了“参数无效"; VB.NET中的错误从MS Access数据库中将图像检索到PictureBox时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VB.NET中使用以下代码从MS ACCESS数据库中检索具有OLE OBJECT类型的图像字段的特定用户图像

I use the following code in VB.NET to retrieve particular user image from MS ACCESS database with the image field of type OLE OBJECT

                 Dim strSql As String = ""
                'For Image
                strSql = "Select pic from emp_tb WHERE userid='" + textbox1.Text + "'"

                Dim sqlCmd As New OleDbCommand(strSql, con)

                'Get image data from DB
                Dim imageData As Byte() = DirectCast(sqlCmd.ExecuteScalar(), Byte())

                'Initialize image variable 
                Dim newImage As Image = Nothing

                If Not imageData Is Nothing Then
                    'Read image data into a memory stream 
                    Using ms As New MemoryStream(imageData, 0, imageData.Length)
                        ms.Write(imageData, 0, imageData.Length)
                        'Set image variable value using memory stream. 
                        newImage = Image.FromStream(ms, True)

                    End Using

                End If

我将其检索到图片框

picturebox1.image=newImage

我的错误:

第300行是

newImage = Image.FromStream(ms, True)

但是我得到的错误消息为参数无效" .请帮助我如何使用参数避免sql注入以及如何解决此错误.....

But I get the error message as "Perameter not valid".Please help me how can i use parameter for avoid sql injection and also how to solve this error........

推荐答案

RE:尝试加载图像时出现参数无效"错误

在Access中打开数据库文件,然后打开包含要显示的图像的表.

Open the database file in Access, then open the table containing the images you want to display.

  • 如果图像列显示说明Long binary data,则图像已保存为原始二进制数据(有时称为"BLOB"),您应该能够使用现有代码(或其他非常有用的代码)靠近它)以填充PictureBox.

  • If the image column shows the description Long binary data then the images have been saved as raw binary data (sometimes referred to as a "BLOB") and you should be able to use your existing code (or something very close to it) to populate the PictureBox.

但是,如果图像列显示位图图像"或包装"之类的说明,则图像已存储为OLE嵌入式对象.如果您提取原始二进制数据(如您的代码一样)并尝试直接将其转换为图像,则您将收到错误消息,因为二进制数据包括包围实际图像数据的OLE包装器". (有关更多详细信息,请参见我的答案此处.)

However, if the image column displays a description like "Bitmap Image" or "Package" then the images have been stored as OLE embedded objects. If you extract the raw binary data (as your code does) and try to directly convert that to an image you will receive errors because the binary data includes the OLE "wrapper" that surrounds the actual image data. (For more details see my answer here.)

关于如何删除OLE包装器"并检索原始图像数据,已经有很多讨论,但是不幸的是OLE非常复杂,并且(显然)没有特别好的文档.阅读了数十个线程之后,共识似乎是:

There have been many, many discussions about how to remove the OLE "wrapper" and retrieve the raw image data, but unfortunately OLE is very convoluted and (apparently) not particularly well-documented. After reading dozens of threads the consensus seems to be:

  1. 如果您要在Access自身中存储和检索图像 ,则只需让Access做它的事"并处理所有OLE复杂性,你.

  1. If you want to store and retrieve images from within Access itself then just let Access "do its thing" and deal with all of the OLE complexities for you.

如果要从任何其他应用程序中检索图像 ,请确保将图像存储为原始二进制数据(即,不要从Access本身保存图像.)

If you will be retrieving images from any other application then make sure that you store the images as raw binary data (i.e., do not save the images from within Access itself).

尝试涵盖以上两种使用情况可能是真正的麻烦.最好只坚持一个.

Trying to cover both of the above usage cases can be a real nuisance. It is best just to stick with one.

如果您需要从Access数据库中提取OLE包装"对象,则Stephen Lebans的 OLEtoDisk 实用程序可能被证明非常有用.

If you need to extract OLE "wrapped" objects from an Access database then Stephen Lebans' OLEtoDisk utility could prove to be extremely useful.

RE:使用参数化查询来防止SQL注入(和其他麻烦)

那很容易.只需将您的代码更改为...

That's easy. Just change your code to...

strSql = "Select pic from emp_tb WHERE userid=?"

Dim sqlCmd As New OleDbCommand(strSql, con)
sqlCmd.Parameters.AddWithValue("?", textbox1.Text)

'Get image data from DB
Dim imageData As Byte() = DirectCast(sqlCmd.ExecuteScalar(), Byte())

这篇关于“参数无效"; VB.NET中的错误从MS Access数据库中将图像检索到PictureBox时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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