上次你帮我回复了一段 往SQL数据库里写.JPG图片的代码,运行有些问题,能帮忙解释下吗。
Private Sub cmdSave_Click()
Dim rs As New ADODB.Recordset
Dim InterID As String
Dim strData As String
Dim fileNo As Integer
Dim LenF As Long
Dim BMPData() As Byte
Dim mFileName As String
'先保存图片
mFileName = "c:\tt.jpg"
SavePicture Picture1.Image, mFileName
fileNo = FreeFile()
'打开文件取出图形数据
Open mFileName For Binary As #fileNo
LenF = LOF(fileNo)
ReDim BMPData(LenF)
Get #fileNo, , BMPData
Close #fileNo
'保存到数据库中
With rs
.ActiveConnection = sConnection
.LockType = adLockOptimistic
.Open "select * from test "
.AddNew
'字段FID为一个 int 型的主键
'字段FData为一个image 型的字段
.Fields("FID") = InterID
.Fields("FData").AppendChunk BMPData
.Update
End With
rs.Close
Set rs = Nothing
End Sub
'得到
Private Sub cmdLoad_Click()
On Error GoTo H_Error
Dim rsData As New ADODB.Recordset
Dim strData As String, strBuffer As String
Dim LenF As Long
Dim BMPData() As Byte
'打开数据库
With rsData
.ActiveConnection = sConnection
.CursorLocation = adUseClient
.Open "select * from test"
'得到数据的长度
LenF = .Fields("FData").ActualSize
ReDim BMPData(LenF)
'得到图形文件的数据
BMPData = .Fields("FData").GetChunk(LenF)
End With
rsData.Close
Set rsData = Nothing
strBuffer = "c:\test.jpg"
If Dir(strBuffer) <> "" Then
Kill strBuffer
End If
Dim fileNo As Integer
fileNo = FreeFile()
Open strBuffer For Binary As #fileNo
Put #fileNo, , BMPData
Close #fileNo
Picture1.Picture = LoadPicture(strBuffer)
Exit Sub
H_Error:
Debug.Assert False
End Sub运行到 .ActiveConnection = sConnection 出现 实时错误 3001 参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突。麻烦高手看看我这里的错误在那里。
Private Sub cmdSave_Click()
Dim rs As New ADODB.Recordset
Dim InterID As String
Dim strData As String
Dim fileNo As Integer
Dim LenF As Long
Dim BMPData() As Byte
Dim mFileName As String
'先保存图片
mFileName = "c:\tt.jpg"
SavePicture Picture1.Image, mFileName
fileNo = FreeFile()
'打开文件取出图形数据
Open mFileName For Binary As #fileNo
LenF = LOF(fileNo)
ReDim BMPData(LenF)
Get #fileNo, , BMPData
Close #fileNo
'保存到数据库中
With rs
.ActiveConnection = sConnection
.LockType = adLockOptimistic
.Open "select * from test "
.AddNew
'字段FID为一个 int 型的主键
'字段FData为一个image 型的字段
.Fields("FID") = InterID
.Fields("FData").AppendChunk BMPData
.Update
End With
rs.Close
Set rs = Nothing
End Sub
'得到
Private Sub cmdLoad_Click()
On Error GoTo H_Error
Dim rsData As New ADODB.Recordset
Dim strData As String, strBuffer As String
Dim LenF As Long
Dim BMPData() As Byte
'打开数据库
With rsData
.ActiveConnection = sConnection
.CursorLocation = adUseClient
.Open "select * from test"
'得到数据的长度
LenF = .Fields("FData").ActualSize
ReDim BMPData(LenF)
'得到图形文件的数据
BMPData = .Fields("FData").GetChunk(LenF)
End With
rsData.Close
Set rsData = Nothing
strBuffer = "c:\test.jpg"
If Dir(strBuffer) <> "" Then
Kill strBuffer
End If
Dim fileNo As Integer
fileNo = FreeFile()
Open strBuffer For Binary As #fileNo
Put #fileNo, , BMPData
Close #fileNo
Picture1.Picture = LoadPicture(strBuffer)
Exit Sub
H_Error:
Debug.Assert False
End Sub运行到 .ActiveConnection = sConnection 出现 实时错误 3001 参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突。麻烦高手看看我这里的错误在那里。
*****************************************************************************
欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
万分感谢。
首先,添加引用Microsoft ActiveX Data Objects 2.5 Library
例1:把图片文件存入数据库
Dim cn As New ADODB.Connection
cn.Open "DBQ=E:\db.mdb;Driver={Microsoft Access Driver (*.mdb)};"
Dim s As New ADODB.Stream
Dim rs As New ADODB.Recordset
rs.Open "图片表", cn, adOpenDynamic, adLockOptimistic, adCmdTable
s.Type = adTypeBinary
s.Open
s.LoadFromFile "e:\Camcord.bmp"
rs.AddNew
rs.Fields("image").Value = s.Read()
rs.Update
例2:把数据库中的图像数据导出为文件
Dim cn As New ADODB.Connection
cn.Open "DBQ=E:\db.mdb;Driver={Microsoft Access Driver (*.mdb)};"
Dim s As New ADODB.Stream
Dim rs As New ADODB.Recordset
rs.Open "图片表", cn, adOpenDynamic, adLockOptimistic, adCmdTable
s.Type = adTypeBinary
s.Open
s.Write rs.Fields("image").Value
s.SaveToFile "e:\xxx.bmp"
注:1.图片表的image字段类型为OLE 对象。
cn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind;Data Source=servername" Dim rs As New ADODB.Recordset
With rs
.ActiveConnection =cn
.CursorLocation = adUseClient
.Open "select * from 图片表"
.AddNew fileNo = FreeFile()
'打开文件取出图形数据
Open "e:\Camcord.bmp" For Binary As #fileNo
LenF = LOF(fileNo)
ReDim BMPData(LenF)
Get #fileNo, , BMPData
Close #fileNo '字段image为一个image 型的字段
.Fields("image").AppendChunk BMPData
.Update
End With
rs.Close
*****************************************************************************
欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
'得到
Private Sub cmdLoad_Click()
On Error GoTo H_Error
Dim rsData As New ADODB.Recordset
Dim strData As String, strBuffer As String
Dim LenF As Long
Dim BMPData() As Byte
'打开数据库
With rsData
.ActiveConnection = sConnection
.CursorLocation = adUseClient
.Open "select * from test"
'得到数据的长度
LenF = .Fields("FData").ActualSize
ReDim BMPData(LenF)
'得到图形文件的数据
BMPData = .Fields("FData").GetChunk(LenF)
End With
rsData.Close
Set rsData = Nothing
strBuffer = "c:\test.jpg"
If Dir(strBuffer) <> "" Then
Kill strBuffer
End If
Dim fileNo As Integer
fileNo = FreeFile()
Open strBuffer For Binary As #fileNo
Put #fileNo, , BMPData
Close #fileNo
Picture1.Picture = LoadPicture(strBuffer)
Exit Sub
H_Error:
Debug.Assert False
End Sub
运行到 {Set rsData = Nothing} 时就先显示WITH没有设置, 我就把他注释掉了 就可以用了
也不知道是为什么 也不明白那句话是做什么用的。