Open VB6 and create a project.
Add two buttons, an image control
Note: The photo field type in access is OLE object.
The photo field type in sqlserver is image.
.
'** Reference Microsoft ActiveX Data Objects 2.5 library and later
Stream objects are not supported in versions earlier than '2. 5 '.
Dim iconcstr as string
Dim iconc as ADODB. Connection
'Save the file to the database.
Sub s_savefile ()
Dim istm as ADODB. Stream
Dim ire as ADODB. recordset
Dim iconcstr as string
'Read the file to the content
Set istm = new ADODB. Stream
With istm
. Type = adtypebinary 'binary Mode
. Open
. Loadfromfile app. Path + "/test.jpg"
End
'Open the table that saves the file
Set ire = new ADODB. recordset
With ire
. Open "select * From IMG", iconc, 1, 3
. Addnew' adds a record.
. Fields ("photo") = istm. Read
. Update
End
'Close the object after completion
Ire. Close
Istm. Close
End sub
Sub s_readfile ()
Dim istm as ADODB. Stream
Dim ire as ADODB. recordset
'Open a table
Set ire = new ADODB. recordset
'Get the latest record
Ire. Open "select top 1 * From IMG order by id desc", iconc, adopenkeyset, adlockreadonly
'Save to file
Set istm = new ADODB. Stream
With istm
. Mode = admodereadwrite
. Type = adtypebinary
. Open
. Write ire ("photo ")
'Here, if test1.jpg is found in the current directory, an error indicating a failed file writing will be reported.
. Savetofile app. Path & "/test1.jpg"
End
Image1.picture = loadpicture (App. Path & "/test1.jpg ")
'Close the object
Ire. Close
Istm. Close
End sub
Private sub commandementclick ()
Call s_readfile
End sub
Private sub command2_click ()
Call s_savefile
End sub
Private sub form_load ()
'Database connection string
Iconcstr = "provider = Microsoft. Jet. oledb.4.0; persist Security info = false "&_
"; Data Source = F:/csdn_vb/database/Save image/access image/IMG. mdb"
'The following statement is used to connect to the sqlserver database.
'Iconcstr = "provider = sqloledb.1; persist Security info = true ;"&_
'"User ID = sa; Password =; initial catalog = test; Data Source = Yang"
Set iconc = new ADODB. Connection
Iconc. Open iconcstr
End sub
Private sub form_unload (cancel as integer)
Iconc. Close
Set iconc = nothing
End sub
You can also read data in binary mode, but you can see that the stream object is simple and efficient.
You can refer to the MS Knowledge Base Article to enhance this knowledge point:
Http://support.microsoft.com/default.aspx? SCID = KB; en-US; 258038