上次 feiyun0112 帮我回复了一段代码,
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
This example uses the AppendChunk and GetChunk methods to fill an image field with data from another record.'BeginAppendChunkVB
Public Sub AppendChunkX() Dim cnn1 As ADODB.Connection
Dim rstPubInfo As ADODB.Recordset
Dim strCnn As String
Dim strPubID As String
Dim strPRInfo As String
Dim lngOffset As Long
Dim lngLogoSize As Long
Dim varLogo As Variant
Dim varChunk As Variant
Const conChunkSize = 100 ' Open a connection.
Set cnn1 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=MyServer;Initial Catalog=Pubs;User Id=sa;Password=; "
cnn1.Open strCnn
' Open the pub_info table.
Set rstPubInfo = New ADODB.Recordset
rstPubInfo.CursorType = adOpenKeyset
rstPubInfo.LockType = adLockOptimistic
rstPubInfo.Open "pub_info", cnn1, , , adCmdTable
' Prompt for a logo to copy.
Dim strMsg As String
strMsg = "Available logos are : " & vbCr & vbCr
Do While Not rstPubInfo.EOF
strMsg = strMsg & rstPubInfo!pub_id & vbCr & _
Left(rstPubInfo!pr_info, InStr(rstPubInfo!pr_info, ",") - 1) & _
vbCr & vbCr
rstPubInfo.MoveNext
Loop
strMsg = strMsg & "Enter the ID of a logo to copy:"
strPubID = InputBox(strMsg)
' Copy the logo to a variable in chunks.
rstPubInfo.Filter = "pub_id = '" & strPubID & "'"
lngLogoSize = rstPubInfo!logo.ActualSize
Do While lngOffset < lngLogoSize
varChunk = rstPubInfo!logo.GetChunk(conChunkSize)
varLogo = varLogo & varChunk
lngOffset = lngOffset + conChunkSize
Loop
' Get data from the user.
strPubID = Trim(InputBox("Enter a new pub ID" & _
" [must be > 9899 & < 9999]:"))
strPRInfo = Trim(InputBox("Enter descriptive text:")) ' Add the new publisher to the publishers table to avoid
' getting an error due to foreign key constraint.
cnn1.Execute "INSERT publishers(pub_id, pub_name) VALUES('" & _
strPubID & "','Your Test Publisher')"
' Add a new record, copying the logo in chunks.
rstPubInfo.AddNew
rstPubInfo!pub_id = strPubID
rstPubInfo!pr_info = strPRInfo lngOffset = 0 ' Reset offset.
Do While lngOffset < lngLogoSize
varChunk = LeftB(RightB(varLogo, lngLogoSize - lngOffset), _
conChunkSize)
rstPubInfo!logo.AppendChunk varChunk
lngOffset = lngOffset + conChunkSize
Loop
rstPubInfo.Update
' Show the newly added data.
MsgBox "New record: " & rstPubInfo!pub_id & vbCr & _
"Description: " & rstPubInfo!pr_info & vbCr & _
"Logo size: " & rstPubInfo!logo.ActualSize ' Delete new records because this is a demonstration.
rstPubInfo.Requery
cnn1.Execute "DELETE FROM pub_info " & _
"WHERE pub_id = '" & strPubID & "'" cnn1.Execute "DELETE FROM publishers " & _
"WHERE pub_id = '" & strPubID & "'" rstPubInfo.Close
cnn1.CloseEnd Sub
'EndAppendChunkVB