将图像插入访问数据库时出错 [英] Error while inserting image into access database

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

问题描述

大家好,

我正在尝试将图像插入到MS Access 2007数据库中。我选择的数据类型是OLEObject,Fieldname是Image。

我尝试了下面的代码,按下按钮时执行:

 Private Sub ButtonPress()

Dim cmd As New OleDbCommand
Dim MemStream As New IO.MemoryStream
Dim DataPic_Update As Byte()
Dim strImage As String

如果不是IsNothing(PictureBox1.Image)那么

PictureBox1.Image.Save(MemStream,Imaging.ImageFormat.Png)
DataPic_Update = MemStream.GetBuffer
MemStream .Read(DataPic_Update,0,MemStream.Length)
strImage =?
MemStream.Close()

否则
DataPic_Update = Nothing
strImage =NULL
结束如果

con。 Open()

cmd.CommandText =INSERT INTO Inventory([Image])+VALUES(@Image)

cmd.Parameters.Add(@ Image ,OleDbType.Binary).Value = DataPic_Update
cmd.Connection = con
cmd.ExecuteNonQuery()
con.Close()

End Sub





执行命令ExecuteNonQuery时,我收到以下错误:



标准表达式中的数据类型不匹配。



我无法解决此错误。有人可以帮我解决现有代码中的任何建议或修改吗?

我想插入图像然后从访问数据库中检索。



谢谢,

Raj S.

解决方案

嗨会员,





而不是这一行

 cmd.Parameters.Add(  @ Image,OleDbType.Binary).Value = DataPic_Update 





你可以在线下试试



 cmd.Parameters.Add(   @ Image,OleDbType.LongVarBinary).Value = DataPic_Update 





我希望这会对你有所帮助。



谢谢

Mohan G


这对你有没有帮助我不知道但是

i想说缺少一行



cmd.displose



它将在con.close之前写入


你的命令应该是:

 cmd.CommandText =   INSERT INTO Inventory([Image])VALUES(@Image) 



也在你的密码中

 DataPic_Update = MemStream.GetBuffer 
MemStream.Read(DataPic_Update, 0 ,MemStream.Length)' 此行不是必需的,因为您已经在上面的代码行中从Stream重新获取字节。



还要确保在Image列字段的数据库数据类型为 Binary



有关更多信息,请参阅以下链接它会对你有所帮助:

http://www.sattsoft.com/sourcecodes/details/1/2/save-and-retrieve-image-from-access-database-using-vb-net.html [ ^ ]

我希望它会对你有所帮助。 :)


Hi All,
I am trying to insert an image into my MS Access 2007 database. The datatype I chose is "OLEObject" and Fieldname as "Image".
I tried the following code which executes when a button is pressed:

Private Sub ButtonPress()

    Dim cmd As New OleDbCommand
    Dim MemStream As New IO.MemoryStream
    Dim DataPic_Update As Byte()
    Dim strImage As String
    
    If Not IsNothing(PictureBox1.Image) Then
    
        PictureBox1.Image.Save(MemStream, Imaging.ImageFormat.Png)
        DataPic_Update = MemStream.GetBuffer
        MemStream.Read(DataPic_Update, 0, MemStream.Length)
        strImage = "?"
        MemStream.Close()
    
    Else
        DataPic_Update = Nothing
        strImage = "NULL"
    End If
    
    con.Open()
    
    cmd.CommandText = "INSERT INTO Inventory([Image])" + "VALUES(@Image)"
    
    cmd.Parameters.Add("@Image", OleDbType.Binary).Value = DataPic_Update
    cmd.Connection = con
    cmd.ExecuteNonQuery()
    con.Close()

End Sub



While executing the command "ExecuteNonQuery", I am getting following Error:

"Data type mismatch in criteria expression."

I am not able to solve this error. Can someone please help me with any suggestions or modifications required in my existing code?
I want to insert the image and then retrieve from the access database.

Thanks,
Raj S.

解决方案

Hi member,


Instead of this line

cmd.Parameters.Add("@Image", OleDbType.Binary).Value = DataPic_Update



you can try below line

cmd.Parameters.Add("@Image", OleDbType.LongVarBinary).Value = DataPic_Update



I hope this will help you.

Thank's
Mohan G


this is will help u or not i dont know but
i want to say that there is missing one line

cmd.displose

it will written before con.close


Your command should be :

cmd.CommandText = "INSERT INTO Inventory([Image]) VALUES(@Image)"


Also In Your Code

DataPic_Update = MemStream.GetBuffer
MemStream.Read(DataPic_Update, 0, MemStream.Length) 'This Line Not Required because you are already retriving bytes form Stream on above line of code.


Also ensure that in database data type of Image column field is Binary.

For more information see following link. It will help you :
http://www.sattsoft.com/sourcecodes/details/1/2/save-and-retrieve-image-from-access-database-using-vb-net.html[^]
I hope it will help you. :)


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

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