设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1932|回复: 0
打印 上一主题 下一主题

[ADO/DAO] 【源码】VBA/VB读取oracle数据库中的图片(BLOB),以数据流的方式

[复制链接]

点击这里给我发消息

跳转到指定楼层
1#
发表于 2015-6-18 10:31:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Public Sub Combo1_Click()

Dim adoCnn As ADODB.Connection
    Dim rstOra As ADODB.Recordset
    Dim intI   As Integer

    Set adoCnn = New ADODB.Connection
    Set rstOra = New ADODB.Recordset

    adoCnn.ConnectionString = "Provider=OraOLEDB.Oracle;User ID=dzjc;password=zlkj;Data Source=dzjc_2005;" '读blob字段要用Provider=OraOLEDB.Oracle

    adoCnn.CursorLocation = adUseClient
    adoCnn.Open

    rstOra.CursorLocation = adUseClient

    rstOra.ActiveConnection = adoCnn
    rstOra.Open "SELECT zp FROM dzjc.dzjc_wfzp WHERE xh = '5'"

    'Set Image1.DataSource = rstOra

    Set Image1.Picture = PictureFromByteStream(rstOra.Fields("zp").Value)

End Sub

模块代码

Private Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, ppstm As Any) As Long
Private Declare Function OleLoadPicture Lib "olepro32" (pStream As Any, ByVal lSize As Long, ByVal fRunmode As Long, riid As Any, ppvObj As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As Any) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long

Public Type GUID ' 16 bytes (128 bits)
'dwData1 As Long ' 4 bytes
'wData2 As Integer ' 2 bytes
'wData3 As Integer ' 2 bytes
abData4(7) As Byte ' 8 bytes, zero based
End Type


Public Function PictureFromByteStream(B() As Byte) As IPicture
    Dim hMem        As Long
    Dim lpMem       As Long
    Dim LowerBound  As Long
    Dim ByteCount   As Long
    Dim IID_IPicture As GUID
    Dim istm        As stdole.IUnknown

    LowerBound = LBound(B)
    ByteCount = UBound(B) - LowerBound + 1

    hMem = GlobalAlloc(&H2, ByteCount)

    If hMem <> 0 Then

        lpMem = GlobalLock(hMem)

        If lpMem <> 0 Then

            MoveMemory ByVal lpMem, B(LowerBound), ByteCount

            GlobalUnlock hMem

            If CreateStreamOnHGlobal(hMem, 1, istm) = 0 Then

                If CLSIDFromString(StrPtr("{7BF80980-BF32-101A-8BBB-00AA00300CAB}"), IID_IPicture) = 0 Then

                    OleLoadPicture ByVal ObjPtr(istm), ByteCount, 0, IID_IPicture, PictureFromByteStream

                End If

            End If

        End If

    End If

End Function

摘自
heisetoufa 博客


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-18 18:42 , Processed in 0.079249 second(s), 24 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表