vb 6.0将图像位图插入ms访问数据库

时间:2011-04-12 21:27:13

标签: database image ms-access vb6 blob

为什么我的图片没有插入?这是我的代码。

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

我收到了“已完成”msgbox但未插入图片!!!!

2 个答案:

答案 0 :(得分:3)

我通常使用ADODB.Stream进行此类操作 - 我发现它比分块方法更容易理解。

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

从ADO版本2.5添加ADODB.Stream:
ADO Version History
ADO Stream Documentation

答案 1 :(得分:1)

您必须在结构化存储中保留位图,以使MS Access绑定按预期工作。

查看Edanmo的Load and save pictures to byte arrays.示例,了解以兼容方式进行序列化的方法。然后,您可以使用简单分配来更新记录集字段(如果它是客户端字段)。