Read and write the image in the database with VB6

zhaozj2021-02-16  53

1. Taking the name and related picture as an example, the database is Access, there is the following field: name char, Picture Ole Object, FileLength

Number. When it is MS SQL, change the Picture to LOB.

2, the example contains Control: Commm Dialog, Picture, Listbox.

The source code is as follows:

Option expedition

Private Declare Function GetTempFileName LIB "kernel32" Alias ​​"getTempFileName" (Byval Lpszpath As

String, Byval LpprefixString As String, ByVal Wunique Ashang, 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

Note: Return A Temporary File Name.

Private function temporaryfilename () AS String

DIM TEMP_PATH AS STRING

DIM TEMP_FILE AS STRING

DIM Length As Long

Note: Get The Temporary File Path.

Temp_path = space $ (MAX_PATH)

Length = getTemppath (max_path, temp_path)

TEMP_PATH = Left $ (Temp_Path, Length)

Note: 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 flow_load ()

DIM DB_FILE AS STRING

DIM RS as adodb.recordset

Note: Get The Database File Name.

DB_FILE = app.path

IF Right $ (DB_FILE, 1) <> "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

DB_FILE = DB_FILE & "dbpict.mdb"

Note: 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"

Note: Get the list of people.

SET RS = m_dbconn.execute ("Select Name from People 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

Note: 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

Note: Get The Record.

SET RS = m_dbconn.execute ("SELECT * from people where name = Note:" & _

Lstpeople.Text & "Note:", AdcmdText)

IF RS.EOF THEN EXIT SUB

Note: Get a Temporary File Name.

FILE_NAME = TemporaryFileName ()

Note: Open the file.

FILE_NUM = FreeFile

Open file_name for binary as #file_num

Note: Copy The Data Into The File.

File_length = rs! FileLength

Num_blocks = file_ley / 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

Note: Display The Picture File.

PicPerson.Picture = loadingPicture (file_name)

PicPerson.visible = true

Width = PicPerson.Left PicPerson.width width - Scalewidthhgt = 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 _

CDLOFNEXPLORERERER

DLGPICTURE.CANCELERROR = TRUE

DLGPICTURE.FILTER = "Graphics Files | * .bmp; *. ICO; *. jpg; *. gif"

ON Error ResMe 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

Note: 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_ley / 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

转载请注明原文地址:https://www.9cbs.com/read-27486.html

New Post(0)