Save and display images using stream objects

Source: Internet
Author: User

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

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.