📄 form1.frm
字号:
VERSION 5.00
Object = "{67397AA1-7FB1-11D0-B148-00A0C922E820}#6.0#0"; "MSADODC.OCX"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form Form1
ClientHeight = 5685
ClientLeft = 60
ClientTop = 345
ClientWidth = 6165
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 5685
ScaleWidth = 6165
StartUpPosition = 3 '窗口缺省
Begin MSAdodcLib.Adodc Adodc1
Height = 495
Left = 120
Top = 3000
Width = 5775
_ExtentX = 10186
_ExtentY = 873
ConnectMode = 0
CursorLocation = 3
IsolationLevel = -1
ConnectionTimeout= 15
CommandTimeout = 30
CursorType = 3
LockType = 3
CommandType = 2
CursorOptions = 0
CacheSize = 50
MaxRecords = 0
BOFAction = 0
EOFAction = 0
ConnectStringType= 1
Appearance = 1
BackColor = -2147483643
ForeColor = -2147483640
Orientation = 0
Enabled = -1
Connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=照片库.mdb;Persist Security Info=False"
OLEDBString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=照片库.mdb;Persist Security Info=False"
OLEDBFile = ""
DataSourceName = ""
OtherAttributes = ""
UserName = ""
Password = ""
RecordSource = "employee"
Caption = "Adodc1"
BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
Name = "宋体"
Size = 9
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
_Version = 393216
End
Begin VB.PictureBox PicBox
DataField = "Photo"
DataSource = "Adodc1"
Height = 1695
Left = 120
ScaleHeight = 1635
ScaleWidth = 3795
TabIndex = 9
Top = 1200
Width = 3855
End
Begin VB.CommandButton cmdUpdate
Caption = "更新"
Height = 615
Left = 2760
TabIndex = 6
Top = 4920
Width = 1455
End
Begin VB.CommandButton cmdSave
Caption = "保存"
Height = 615
Left = 1080
TabIndex = 5
Top = 4920
Width = 1455
End
Begin VB.CommandButton cmdPreView
Caption = "预览"
Height = 615
Left = 3000
TabIndex = 4
Top = 3960
Width = 1455
End
Begin MSComDlg.CommonDialog CommonDialog1
Left = 4800
Top = 3840
_ExtentX = 847
_ExtentY = 847
_Version = 393216
End
Begin VB.TextBox txtAddId
Height = 375
Left = 1080
TabIndex = 3
Top = 4320
Width = 1575
End
Begin VB.TextBox txtAddName
Height = 375
Left = 1080
TabIndex = 2
Top = 3840
Width = 1575
End
Begin VB.Label Label6
Caption = "添加编号"
Height = 255
Left = 120
TabIndex = 11
Top = 4320
Width = 855
End
Begin VB.Label Label5
Caption = "添加姓名"
Height = 255
Left = 120
TabIndex = 10
Top = 3960
Width = 855
End
Begin VB.Label lblID
BackColor = &H8000000B&
BorderStyle = 1 'Fixed Single
DataField = "ID"
DataSource = "Adodc1"
Height = 375
Left = 1080
TabIndex = 8
Top = 720
Width = 1335
End
Begin VB.Label lblName
BackColor = &H8000000B&
BorderStyle = 1 'Fixed Single
DataField = "Name"
DataSource = "Adodc1"
Height = 375
Left = 1080
TabIndex = 7
Top = 240
Width = 1335
End
Begin VB.Label Label2
Caption = "编号:"
Height = 255
Left = 240
TabIndex = 1
Top = 720
Width = 735
End
Begin VB.Label Label1
Caption = "姓名:"
Height = 255
Left = 240
TabIndex = 0
Top = 240
Width = 735
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim Constr As String 'ODBC路径
Dim FileName As String '图片文件名
Const BLOCKSIZE = 4096 '每次读写块的大小
Dim ADOCon As New ADODB.Connection 'ADODB Connection对象
Dim ADORst As New ADODB.Recordset 'ADODB Recordset 对象
Dim ADOFld As ADODB.Field 'ADODB Field 对象
Private Sub SaveToDB(ByRef Fld As ADODB.Field, DiskFile As String)
Dim byteData() As Byte '定义数据块数组
Dim NumBlocks As Long '定义数据块个数
Dim FileLength As Long '标识文件长度
Dim LeftOver As Long '定义剩余字节长度
Dim SourceFile As Long '定义自由文件号
Dim i As Long '定义循环变量
SourceFile = FreeFile '提供一个尚未使用的文件号
Open DiskFile For Binary Access Read As SourceFile '打开文件
FileLength = LOF(SourceFile) '得到文件长度
If FileLength = 0 Then '判断文件是否存在
Close SourceFile
MsgBox DiskFile & " 无 内 容 或 不 存 在 !"
Else
NumBlocks = FileLength \ BLOCKSIZE '得到数据块的个数
LeftOver = FileLength Mod BLOCKSIZE '得到剩余字节数
Fld.Value = Null
ReDim byteData(BLOCKSIZE) '重新定义数据块的大小
For i = 1 To NumBlocks
Get SourceFile, , byteData() ' 读到内存块中
Fld.AppendChunk byteData() '写入FLD
Next i
ReDim byteData(LeftOver) '重新定义数据块的大小
Get SourceFile, , byteData() '读到内存块中
Fld.AppendChunk byteData() '写入FLD
Close SourceFile '关闭源文件
End If
End Sub
Private Sub cmdPreView_Click()
'显示打开文件的公用对话框,选择需要加入数据库的图片
CommonDialog1.Filter = "图片文件|*.bmp;*.ico;*.jpg;*.gif;*.jpeg"
CommonDialog1.ShowOpen
FileName = CommonDialog1.FileName
PicBox.Picture = LoadPicture(FileName) '预览图片
End Sub
Private Sub cmdSave_Click()
ADORst.AddNew '新增纪录
ADORst("Name").Value = txtAddName.Text '给动态集的第一个字段赋值
ADORst("ID").Value = txtAddId.Text '给动态集的第二个字段赋值
Set ADOFld = ADORst("Photo") '给ADODB.Field对象赋值
Call SaveToDB(ADOFld, FileName) '调用子程序,给第3个字段(image)赋值
ADORst.Update
End Sub
Private Sub cmdUpdate_Click()
'重新打开记录集,刷新纪录
ADORst.Close
ADOCon.Close
Set ADORst = Nothing
Set ADOCon = Nothing
ADOCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "" _
& "\照片库.mdb;Persist Security Info=False"
ADORst.Open "select * from employee", ADOCon, adOpenDynamic, adLockOptimistic
Set Adodc1.Recordset = ADORst
End Sub
Private Sub Form_Load()
ADOCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "" _
& "\照片库.mdb;Persist Security Info=False"
ADORst.Open "select * from employee", ADOCon, adOpenDynamic, adLockOptimistic
'打开一个ADO动态集 表名为table
Set Adodc1.Recordset = ADORst '将动态集赋给ADO控件
End Sub
Private Sub Form_Unload(Cancel As Integer)
'记得关闭打开的数据集,释放资源
ADORst.Close
ADOCon.Close
Set ADORst = Nothing
Set ADOCon = Nothing
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -