The database is the latest technology of data management. It is an important branch of computer science. It is the basis and core of modern computer information systems and computer applications. Today, in the high-speed development of science and technology, there is everywhere in information resources, everywhere, has become an important wealth of various departments, is especially important for people engaged in procedures.
Today, the operation of the database is not only a single operation of characters and numbers, but the storage and display of images is particularly important. The author described below will introduce two images display and storage by VB6.0 and Access 97 as a development tool.
Use data controls and data binding controls
Using this method, you can construct a simple database application if you don't write or write a small amount of code, which is easy to be accepted by beginners. Before an example, first describe data binding functions, all controls with DataSource properties are sensitive to data, they can directly use data in the database through data controls. For example, Checkbox Control, Combobox Comtrol, TextBox Comtrol, Picturebox Control, Image Comtrol ... Because of this way, it is relatively less than understanding, it is more likely to understand, not explained, now introduce the programming steps.
1. Display the image required from the database
First, add a DATA data control, set its DatabaseName and RecordSource properties,
StrPath = app.pathif Right (StrPath, 1) <> "/" damath = strpath & "/ "mydata.DatabaseName = strpath &" exampledb.mdb "data stock address mydata.recordsource =" info "table name
Step 2, add an image control to display the image, set its DataSource and DataField properties. For example this example: image1.datasource = "mydata" and image1.datafield = "MyPhoto". Then set the other controls with the data binding function to display other contents of the other. After these two steps, the program can display the data you want.
2. Add a picture that needs to be stored in the database
First, use the AddNew property of the data control to add a button, double-click, add the following code mydata.recordset.addnew
Step 2, specify image path to image control images image1.picture = loadingPicture ("Image Path"), after the two steps, you can add a picture to the database.
This method is the easiest and fast, and there is very little code to write. However, this method has certain restrictions in operation speed and flexibility, suitable for beginners and some simple applications, to be flexible and variable display images, the methods described below may be more suitable for your requirements.
Use the write code to implement the storage and display of the image
This method is relative to the method, the amount of code is large, but its operation is flexible, which can meet the various forms of operation, which is favored by more programming. However, the knowledge involved is more relatively more, but also to master the operation method of the database, but also further understanding of the reading and writing of binary files. About the basic operation of the database and binary files Many reference books are described in detail, please check it. The variables used in this section before programming as follows:
Dim RS As New ADODB.Recordset Dim Chunk () As ByteConst ChunkSize As Integer = 2384Dim DataFile As Integer, Chunks, Fragment As IntegerDim MediaTemp As StringDim lngOffset, lngTotalSize As LongDim i As Integer1, desired display image from the database
The first step first opens the database, see if there is any content to find, and it will continue to execute, not to exit
Rs.Source = "SELECT * from info where name = '" & sparaname ";" rs.activeconnection = "uid =; pwd =; dsn = testdb;" RS.Openif Rs.EOF THEN RS.CCOSE: EXIT SUB
In the second step, read the long binary data, the picture data, convert it into a picture file, the operation process is as follows
MediaTemp = strPath & "picturetemp.tmp" DataFile = 1Open MediaTemp For Binary Access Write As DataFilelngTotalSize = RS! MyPhoto.ActualSizeChunks = lngTotalSize / ChunkSizeFragment = lngTotalSize Mod ChunkSizeReDim Chunk (Fragment) Chunk () = RS! MyPhoto.GetChunk (Fragment) Put DataFile, Chunk () for i = 1 to chunksredim chunk (chunksize) chunk () = rs! Myphoto.getchunk (chunksize) Put DataFile, Chunk () Next iClose DataFile
In the third step, turn off the database so that you can display the picture you want.
Rs.closeif mediatemp = "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "
2. Add a picture that needs to be stored in the database
Adding a stored picture to the database is a picture reverse process. As long as you have the operation of displaying the picture, the operation of the storage picture will be solved, and the steps will be described below.
The first step first turns on the database, the process is as follows:
Rs.Source = "SELECT * from info;" rs.cursortype = adopenkeysetrs.locktype = adlockoptimisticrs.activeConnection = "uid =; pwd =; dsn = testdb;" rs.open
In the second step, convert the picture you want to store into binary long files into the database, the operation process is as follows
RS.AddNewDataFile = 1Open strPathPicture For Binary Access Read As DataFileFileLen = LOF (DataFile) 'the data length of a file If FileLen = 0 Then: Close DataFile: RS.Close: Exit SubChunks = FileLen / ChunkSizeFragment = FileLen Mod ChunkSizeReDim Chunk (Fragment) Get DataFile, Chunk () rs! Myphoto.appendchunk chunk () redim chunk (chunksize) for i = 1 to chunks get datafile, chunk () rs! Myphoto.appendchunk chunk () Next iClose DataFile third step, update record To turn off the database, complete the data image to the database.
Rs.Updaters.closset = nothing
Both methods have their own aspects, and readers can make reasonable choices for their own conditions.