First, the design of the database
The database can be performed using Microsoft Access 97 or SQL Server. First, create a table, name Table, add three fields, named: Name CHAR type (SQL Server) text type (Access); No. CHAR type In SQL Server) Text Type (Access); photo image type (SQL Server) OLE object (in Access), the rear storage is designed. In order to be able to remotely, we use the ODBC method to do, double-click ODBC data source in the Control Panel, click the System DSN tab, press the "Add" button to select the corresponding data source driver Access * .mdb or SQL Server, add the data source according to the addition wizard, and you can start programming.
Second, the writing of the program
Run VB and create a new project. This program uses ADO controls and dynamic link libraries to access the database, you need to join the ADO runtime, click the Project / References menu, the reference dialog box, select Microsoft ActiveX Data Objects 2.0 Library and determined.
Add a Form, Four Label Controls, Two TextBox controls, a PictureBox control, an AdodC control, three CommandButton controls, a CommandDialog control, if the Adodc and CommandDialog controls are not appeared on the toolbox, please click on the menu "project / part". Click the Controls tab to select the Microsoft Ado Data Control 6.0 (OLEDB) and Microsoft Common Dialog Control 6.0 Press "OK" button.
Below is some properties of the above controls:
Form1.MaxButton = False Label1.Caption = Name: Label2.Caption = No: Label3.Name = ResName Label3.BackColor = & H80000009 & Label3.BorderStyle = 1-Fixed Single Label3.DataField = Label3.DataSource = AdoCtr Label4.Name name = ResNumb Label4.BackColor = & H80000009 & Label4.BorderStyle = 1-Fixed Single Label4.DataField = number Label4.DataSource = AdoCtr Text1.Name = Names Text2.Name = Numb CommonDialog1.Name = CDlg Adodc1.Name = AdoCtr CommonButton1.Name = PreView CommonButton1. Caption = preview CommonButton2.Name = save CommonButton2.Caption = save CommonButton3.Name = update CommonButton3.Caption = update PictureBox1.Name = PicBox PictureBox1.AutoSize = False PictureBox1.AutoRedraw = False PictureBox1.DataField = The following is a photo PictureBox1.DataSource = AdpCtr code:
'
This project requires Microsoft ActiveX Data Object 2.1 Library (Msado15.dll)
Dim Constr As String 'ODBC path Dim FileName As String' image file name Const BLOCKSIZE = 4096 'each write block size Dim ADOCon As New ADODB.Connection' ADODB Connection objects Dim ADORst As New ADODB.Recordset 'ADODB Recordset objects Dim Adofld As Adodb.field 'AdoDB Field Object ----------------------- Private sub savetoDB (Byref Fld As Adod.field, DiskFile As String) DIM BYTEDATA () AS BYTE 'Defines Data Block Array DIM Numblocks As long' Defines Data Block DIM FileLength AS Long 'Label File Length DIM LEFTOVER AS Long' Defines Remaining Byte Length DIM SourceFile As Long 'Defining Free File Number DIM i As Long 'define a loop variable SourceFile = FreeFile' provide an unused file number open DiskFile For Binary Access Read As SourceFile 'open file FileLength = LOF (SourceFile)' get file length if FileLength = 0 Then 'determines whether a file exists Close SourceFile MsgBox DiskFile & "No content or does not exist!" Else Numblocks = filelength / blocksize 'LEFTOVER = filelength mod block blocksize' Get the remaining bytes FLD.VALUE = NULL Redim Bytedata (blockize) 'redefines the size of the data block For i = 1 to Numblocks get sourcefile, Bytedata () 'Reads in the memory block FLD.Appendchunk Bytedata ()' Write FLD Next I redim Bytedata (Leftover) 'Redes the size of the data block get sourcefile, Bytedata ()' reads the memory block fld.appendchunk Bytedata () 'Write FLD Close Sourcefile' Close source file end if End Sub --- -------------------
Private Sub Form_Load () Constr = "DSN = image" 'define an ODBC connection ADOCon.Open Constr' create a connection ADORst.Open "table", ADOCon, adOpenDynamic, adLockOptimistic 'open a set of dynamic ADO table named table Set AdoCtr.Recordset = Adorst 'to cite dynamic to the ADO control END SUB ----------------------
Private Sub Form_unload (Cancel As Integer) "Remember to close the open data set, release the resource adorst.close adocon.close set adorst = Nothing set adocon = Nothing end SUB
----------------------
Private sub preview_click () display Opens the public dialog of the file, select the picture of the database to join the database cdlg.filter = "bitmap (* .bmp) | * .bmp" cdlg.showopen filename = cdlg.FileName Picbox.Picture = loadingPicture (Filename) 'Preview Picture End Sub
----------------------
Private Sub Save_Click () Adorst.addnew 'Added record adorst ("Name") .Value = names.text' assigns ADORST ("Number) .value = Numb.Text 'gives dynamic sets. The second field assignment set adofld = adorst ("photo") 'assigns the Call SavetoDB (Adofld, FileName)' call subroutine (Image) assignment Adorst.update End Sub to the AdoDB.field object
----------------------