vb 6.0 Insert image bitmap to ms access database

天涯浪子 提交于 2019-12-11 11:15:55

问题


Why doesn't my image get inserted? Here is my code.

Sub SaveToDBs(strImagePath As String, fname As String)
rs.Close
rs.Open "Sheet1", conn, adOpenDynamic, adLockBatchOptimistic, adCmdTable
Dim bytBLOB() As Byte
MsgBox strImagePath
Dim intNum As Integer
With rs   
    intNum = FreeFile
    Open strImagePath For Binary As #intNum
    ReDim bytBLOB(FileLen(strImagePath))
    'Read data and close file
    Get #intNum, , bytBLOB
    Close #1
    .Fields(fname).AppendChunk bytBLOB
    .Update
End With
    MsgBox "done"
End Sub

I got "done" msgbox but image not inserted !!!!


回答1:


I normally use ADODB.Stream for this sort of thing - I find it easier to understand than the chunking methods.

Sub SaveToDBs(strImagePath As String, fname As String)
rs.Close
rs.Open "Sheet1", conn, adOpenDynamic, adLockBatchOptimistic, adCmdTable
MsgBox strImagePath
Dim intNum As Integer
Dim myStream as ADODB.Stream
With rs      
    .AddNew
    Set myStream = new ADODB.Stream
    myStream.Type = adTypeBinary
    myStream.LoadFromFile(strImagePath)
    .Fields(fname) = myStream.Read
    .Update
    Set myStream = Nothing
End With
    MsgBox "done"
End Sub

ADODB.Stream was added from ADO version 2.5:
ADO Version History
ADO Stream Documentation




回答2:


You have to persist bitmaps in structured storage for the MS Access binding to work as expected.

Take a look at Edanmo's Load and save pictures to byte arrays. sample for a way to serialize in a compatible way. Then you could use simple assignment to update your recordset field if it's a client side one.



来源:https://stackoverflow.com/questions/5641835/vb-6-0-insert-image-bitmap-to-ms-access-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!