麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

用VB6讀寫數(shù)據(jù)庫中的圖片

2019-11-18 22:12:47
字體:
供稿:網(wǎng)友
很多兄弟在這里問關(guān)于VB6讀寫數(shù)據(jù)庫中的圖片的問題,在此有一例,希有所啟發(fā)。
   1,以人名和相關(guān)圖片為例說明,數(shù)據(jù)庫為access,有如下字段:Name char,picture OLE object,FileLength
Number。當(dāng)為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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费国产a | 久久久久国产成人精品亚洲午夜 | 爱唯侦察 国产合集 亚洲 | 欧美日韩一区,二区,三区,久久精品 | 激情小说区 | 黄色免费在线视频网站 | 九九精品在线观看视频 | 中文字幕极速在线观看 | 欧美一级小视频 | 中文字幕一区在线观看视频 | 视频一区二区精品 | 欧美一级成人一区二区三区 | 国产一级毛片不卡 | 日韩黄色成人 | 中文字幕在线资源 | a级毛片免费观看在线播放 日本aaa一级片 | 国产日韩中文字幕 | 娇喘视频在线观看 | 日韩黄在线观看 | 农村寡妇偷毛片一级 | 午夜av男人的天堂 | 国产自在线 | 黄色一级电影网 | chinese xvideos gay | 国内精品久久久久久2021浪潮 | 欧洲成人综合网 | 国产欧美日韩视频在线观看 | 久久国产免费 | 国产视频在线播放 | www久久久久久 | 午夜视频在线观看免费视频 | 天天舔天天插 | 国产精品一区二区三区在线播放 | 香蕉视频网站在线观看 | 羞羞视频免费入口网站 | 青青草成人影视 | 欧美在线观看视频一区 | 成人毛片网 | 成年免费网站 | 国产一区二区三区四区五区在线 | 欧美 国产 综合 |