首页 > 开发 > 综合 > 正文

用VB6读写数据库中的图片

2024-07-21 02:24:57
字体:
来源:转载
供稿:网友
很多兄弟在这里问关于vb6读写数据库中的图片的问题,在此有一例,希有所启发。
   1,以人名和相关图片为例说明,数据库为access,有如下字段:name char,picture ole object,filelength number。当为ms sql时,将picture改为lob即可。
   2,示例包含control:commom dialog,picture,listbox。
源码如下:
option explicit

private declare function gettempfilename lib "kernel32" alias "gettempfilenamea" (byval lpszpath as string, byval lpprefixstring as string, byval wunique as long, byval lptempfilename as string) as long
private declare function gettemppath lib "kernel32" alias "gettemppatha" (byval nbufferlength as long, byval lpbuffer as string) as long
private const max_path = 260

private m_dbconn as adodb.connection

private const block_size = 10000
' return a temporary file name.
private function temporaryfilename() as string
dim temp_path as string
dim temp_file as string
dim length as long

    ' get the temporary file path.
    temp_path = space$(max_path)
    length = gettemppath(max_path, temp_path)
    temp_path = left$(temp_path, length)

    ' get the file name.
    temp_file = space$(max_path)
    gettempfilename temp_path, "per", 0, temp_file
    temporaryfilename = left$(temp_file, instr(temp_file, chr$(0)) - 1)
end function
private sub form_load()
dim db_file as string
dim rs as adodb.recordset

    ' get the database file name.
    db_file = app.path
    if right$(db_file, 1) <> "/" then db_file = db_file & "/"
    db_file = db_file & "dbpict.mdb"

    ' open the database connection.
    set m_dbconn = new adodb.connection
    m_dbconn.open _
        "provider=microsoft.jet.oledb.4.0;" & _
        "data source=" & db_file & ";" & _
        "persist security info=false"

    ' get the list of people.
    set rs = m_dbconn.execute("select name from people order by name", , adcmdtext)
    do while not rs.eof
        lstpeople.additem rs!name
        rs.movenext
    loop

    rs.close
    set rs = nothing
end sub
private sub form_resize()
    lstpeople.height = scaleheight
end sub


' display the clicked person.
private sub lstpeople_click()
dim rs as adodb.recordset
dim bytes() as byte
dim file_name as string
dim file_num as integer
dim file_length as long
dim num_blocks as long
dim left_over as long
dim block_num as long
dim hgt as single

    picperson.visible = false
    screen.mousepointer = vbhourglass
    doevents

    ' get the record.
    set rs = m_dbconn.execute("select * from people where name='" & _
        lstpeople.text & "'", , adcmdtext)
    if rs.eof then exit sub

    ' get a temporary file name.
    file_name = temporaryfilename()

    ' open the file.
    file_num = freefile
    open file_name for binary as #file_num

    ' copy the data into the file.
    file_length = rs!filelength
    num_blocks = file_length / block_size
    left_over = file_length mod block_size

    for block_num = 1 to num_blocks
        bytes() = rs!picture.getchunk(block_size)
        put #file_num, , bytes()
    next block_num

    if left_over > 0 then
        bytes() = rs!picture.getchunk(left_over)
        put #file_num, , bytes()
    end if

    close #file_num

    ' display the picture file.
    picperson.picture = loadpicture(file_name)
    picperson.visible = true

    width = picperson.left + picperson.width + width - scalewidth
    hgt = picperson.top + picperson.height + height - scaleheight
    if hgt < 1440 then hgt = 1440
    height = hgt

    kill file_name
    screen.mousepointer = vbdefault
end sub

private sub mnurecordadd_click()
dim rs as adodb.recordset
dim person_name as string
dim file_num as string
dim file_length as string
dim bytes() as byte
dim num_blocks as long
dim left_over as long
dim block_num as long

    person_name = inputbox("name")
    if len(person_name) = 0 then exit sub

    dlgpicture.flags = _
        cdlofnfilemustexist or _
        cdlofnhidereadonly or _
        cdlofnexplorer
    dlgpicture.cancelerror = true
    dlgpicture.filter = "graphics files|*.bmp;*.ico;*.jpg;*.gif"

    on error resume next
    dlgpicture.showopen
    if err.number = cdlcancel then
        exit sub
    elseif err.number <> 0 then
        msgbox "error " & format$(err.number) & _
            " selecting file." & vbcrlf & err.description
        exit sub
    end if

    ' open the picture file.
    file_num = freefile
    open dlgpicture.filename for binary access read as #file_num

    file_length = lof(file_num)
    if file_length > 0 then
        num_blocks = file_length / block_size
        left_over = file_length mod block_size

        set rs = new adodb.recordset
        rs.cursortype = adopenkeyset
        rs.locktype = adlockoptimistic
        rs.open "select name, picture, filelength from people", m_dbconn

        rs.addnew
        rs!name = person_name
        rs!filelength = file_length

        redim bytes(block_size)
        for block_num = 1 to num_blocks
            get #file_num, , bytes()
            rs!picture.appendchunk bytes()
        next block_num

        if left_over > 0 then
            redim bytes(left_over)
            get #file_num, , bytes()
            rs!picture.appendchunk bytes()
        end if

        rs.update
        close #file_num

        lstpeople.additem person_name
        lstpeople.text = person_name
    end if
end sub
上一篇:VBS脚本的应用

下一篇:VBArray 对象

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表