关于ado引用access
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 '给动态集的第二个字段赋值
ADORst("price").Value = price.Text
Set ADOFld = ADORst("Photo") '给ADODB.Field对象赋值
Call SaveToDB(ADOFld, FileName) '调用子程序,给第三个字段(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