Use VB6 to read and write pictures in the database

Source: Internet
Author: User
Data | Database A lot of brothers here ask questions about VB6 reading and writing to a database, and here's an example that I hope is inspiring.
1, with names and related pictures as an example, the database is access, with the following fields: Name char,picture OLE object,filelength number. When you are MS SQL, change the picture to lob.
2, the example contains Control:commom Dialog,picture,listbox.
The source code is as follows:
Option Explicit

Private Declare Function gettempfilename Lib "kernel32" Alias "Gettempfilenamea" (ByVal lpszpath as String, ByVal lpprefix string 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 S Tring) 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 ' 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 but 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.