CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2000
    Posts
    2

    Pulling Image from SQL 7

    How to pull a bmp image in a SQL 7 database into a form using VB6.

    l try this but it was error

    ............
    .Open
    End With

    Set rsEmp = cnHR.Execute("Select emppic from pcfl")
    EmpPIC.Picture = rsEmp!EmpPIC
    ............

    Any example?
    Thanks





  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Pulling Image from SQL 7

    paste this code in a module:

    option Explicit

    Const MAX_PATH = 255

    private Const CHUNK_SIZE = 1000

    private Declare Function GetTempPath Lib "kernel32" _
    Alias "GetTempPathA" (byval nBufferLength as Long, _
    byval lpBuffer as string) as Long

    public Function SavePictureToDB(PictControl as Object, _
    RS as Object, FieldName as string) as Boolean

    'PURPOSE: SAVES PICTURE IN IMAGEBOX, PICTUREBOX, OR SIMILAR
    'CONTROL to RECORDSET RS IN FIELD NAME FIELDNAME

    'FIELD TYPE MUST BE binary (OLE OBJECT IN ACCESS)


    'SAMPLE USAGE
    'Dim sConn as string
    'Dim oConn as new ADODB.Connection
    'Dim oRs as new ADODB.Recordset
    '
    '
    'sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDb.MDB;Persist Security Info=false"
    '
    'oConn.Open sConn
    'oRs.Open "SELECT * FROM MYTABLE", oConn, adOpenKeyset, _
    adLockOptimistic
    'oRs.AddNew

    'SavePictureToDB Picture1, oRs, "MYFIELD"
    'oRs.Update
    'oRs.Close

    Dim oPict as StdPicture

    Dim sDir as string
    Dim sTempFile as string
    Dim iFileNum as Integer
    Dim lFileLength as Long

    Dim abBytes() as Byte
    Dim iCtr as Integer

    on error GoTo ErrorHandler
    If Not TypeOf RS is ADODB.Recordset then Exit Function
    set oPict = PictControl.Picture
    If oPict is nothing then Exit Function

    'Save picture to temp file
    sDir = GetTempDir
    If sDir = "" then sDir = "C:\"
    sTempFile = sDir & "0X2341KLZX.dat"
    SavePicture oPict, sTempFile

    'read file contents to byte array
    iFileNum = FreeFile
    Open sTempFile for binary Access Read as #iFileNum
    lFileLength = LOF(iFileNum)
    ReDim abBytes(lFileLength)
    get #iFileNum, , abBytes()
    'put byte array contents into db field
    RS.Fields(FieldName).AppendChunk abBytes()
    Close #iFileNum

    'Don't return false if file can't be deleted
    on error resume next
    Kill sTempFile
    SavePictureToDB = true
    ErrorHandler:
    End Function

    public Function LoadPictureFromDB(PictControl as Object, _
    RS as Object, FieldName as string) as Boolean

    'PURPOSE: LOADS PICTURE, SAVED as binary DATA IN RECORDSET RS,
    'FIELD FieldName to PICTUREBOX, IMAGEBOX (OR CONTROL
    'WITH SIMILAR INTERFACE)


    'SAMPLE USAGE
    'Dim sConn as string
    'Dim oConn as new ADODB.Connection
    'Dim oRs as new ADODB.Recordset
    '
    '
    'sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDb.MDB;Persist Security Info=false"
    '
    'oConn.Open sConn
    'oRs.Open "SELECT * FROM MyTable", oConn, adOpenKeyset,
    ' adLockOptimistic
    'LoadPictureFromDB Picture1, oRs, "MyFieldName"
    'oRs.Close

    Dim oPict as StdPicture
    Dim sDir as string
    Dim sTempFile as string
    Dim iFileNum as Integer
    Dim lFileLength as Long
    Dim abBytes() as Byte
    Dim iCtr as Integer

    on error GoTo ErrorHandler
    If Not TypeOf RS is ADODB.Recordset then Exit Function
    sDir = GetTempDir
    If sDir = "" then sDir = "C:\"
    sTempFile = sDir & "0X2341KLZX.dat"

    If len(Dir$(sTempFile)) > 0 then
    Kill sTempFile
    End If

    iFileNum = FreeFile
    Open sTempFile for binary as #iFileNum
    lFileLength = LenB(RS(FieldName))

    abBytes = RS(FieldName).GetChunk(lFileLength)
    Put #iFileNum, , abBytes()

    Close #iFileNum

    PictControl.Picture = LoadPicture(sTempFile)

    Kill sTempFile
    LoadPictureFromDB = true

    ErrorHandler:
    End Function

    private Function GetTempDir() as string
    Dim sRet as string, lngLen as Long

    'create buffer
    sRet = string(MAX_PATH, 0)

    lngLen = GetTempPath(MAX_PATH, sRet)
    If lngLen = 0 then Exit Function
    GetTempDir = Left$(sRet, lngLen)
    End Function

    'then call these functions from your form like this:

    private Sub Form_Load()
    LoadPictureFromDB EmpPIC, rsEmp, "EmpPIC"
    End Sub




    hope this helps,

    John


    John Pirkey
    MCSD
    http://www.ShallowWaterSystems.com
    http://www.stlvbug.org
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured