给你一个启发
Public Sub SaveToDB()
Dim AdoCnn As New ADODB.Connection
Dim AdoRst As New ADODB.Recordset
Dim fldFilecon As ADODB.Field
Dim fldFileTp As ADODB.Field
Dim mfiletype As String
Dim StrF As String
Dim tmp As Integer
On Error GoTo DbConErr:
If AdoRst.EOF Then
MsgBox "没有发现这条记录", vbInformation, "提示"
AdoRst.Close
AdoCnn.Close
Set AdoRst = Nothing
Set AdoCnn = Nothing
Exit Sub
End If
Set fldFilecon = AdoRst!wdnr
Call FileToBlob(fldFilecon, StrF)
AdoRst.Close
AdoCnn.CommitTrans
AdoCnn.Close
Set AdoRst = Nothing
Set AdoCnn = Nothing
MsgBox "文档入库成功!", vbInformation, "提示"
Exit Sub
DbConErr:
MsgBox "文档入库错误!", vbInformation, "错误提示1"
If Not IsNull(AdoRst) Then
If AdoRst.State = adStateOpen Then
AdoRst.Close
Set AdoRst = Nothing
End If
End If
If Not IsNull(AdoCnn) Then
If AdoCnn.State = adStateOpen Then
AdoCnn.Close
Set AdoCnn = Nothing
End If
End If
End Sub
把图片转换成二进制
Public Sub FileToBlob(ByRef fld As ADODB.Field, filename As String, Optional ChunkSize As Long = 8192)
Dim fnum As Integer, bytesleft As Long, bytes As Long
Dim tmp() As Byte
If (fld.Attributes And adFldLong) = 0 Then
Err.Raise 1001, , "field doesn't support the GetChunk method."
End If
If Dir$(filename) = "" Then Err.Raise 53, , "File not found"
fnum = FreeFile
Open filename For Binary As fnum
bytesleft = LOF(fnum)
Do While bytesleft
bytes = bytesleft
If bytes > ChunkSize Then bytes = ChunkSize
ReDim tmp(1 To bytes) As Byte
Get fnum, , tmp
fld.AppendChunk tmp
bytesleft = bytesleft - bytes
Loop
Close #fnum
End Sub
如果有不明白的的方给我发EMAIL:ZHGJ1728@163.COM
用ado的stream对象,使用方便而且是发展趋势。
是一个存图象到数据库然后读出的例子!
Private Sub ImportBLOB(cn As ADODB.Connection)
Dim rs As New ADODB.Recordset
Dim stm As ADODB.Stream
Set stm = New ADODB.Stream
' Skip any table not found errors
On Error Resume Next
cn.Execute "drop table BinaryObject"
On Error GoTo 0
'Create the BinaryObject table
cn.Execute "create table BinaryObject " & _
"(blob_id int IDENTITY(1,1), " & _
"blob_filename varchar(256), " & _
"blob_object image)"
rs.Open "Select * from BinaryObject where 1=2", cn, adOpenKeyset, adLockOptimistic
'Read the binary files from disk
stm.Type = adTypeBinary
stm.Open
stm.LoadFromFile App.Path & "\BLOBsample.jpg"
End Sub
Private Sub DisplayBLOB(cn As ADODB.Connection)
Dim rs As New ADODB.Recordset
' Select the only image in the table
rs.Open "Select * from BinaryObject where blob_id = 1", cn
' Set the DataSource to the recordset
Set imgBinaryData.DataSource = rs
'Set the DataField to the BLOB field
imgBinaryData.DataField = rs!blob_object.Name