以下函数是用来保存、读取图片的
savetofile是将图片读出来再写入指定的文件中,然后显示出来。
SaveFileDB 是将图片转换成二进制再保存到数据库中。
GetFileType 是获取文件类型的.
以下函数已在ACCESS数据库中通过,但SQL server 数据库时就保存不了!!
请大家帮帮忙,如果函数有错,请给个正确的。谢谢!!
Public Function SaveToFile(strTable As String, strField As String, strFilter As String, strFileName As String) As Boolean
'============================================================
' 过程函数名: CommModule.SaveToFile 类型:Function
' 参数:
' strTable (String) :保存图形数据的表名称
' strField (String) :保存图形数据的字段名称
' strFilter (String) :打开表的过滤字符串,用于定位并确保被打开的表的纪录的唯一性
' strFileName (String) :准备保存的图象的文件名称
' 返回:如果保存成功,返回True,如果失败,返回False
'-------------------------------------------------------------
' 说明:把由GetFromFile函数保存到表中OLE字段的数据还原到文件
'-------------------------------------------------------------
' 修订历史:
'=============================================================
Dim recset As ADODB.Recordset, FileData() As Byte, FileNo As Long, FileSize As Long, strSQL As String
strSQL = "Select " & strField & " From " & strTable & " Where " & strFilter & ";"
Set recset = New ADODB.Recordset
recset.Open strSQL, gConn, adOpenDynamic, adLockOptimistic
SaveToFile = True
If recset(strField).Type <> adLongVarWChar Then
SaveToFile = False '如果字段不是OLE字段,返回错误
GoTo EndSaveTofile
End If
If recset.EOF Then '如果记录不存在,返回错误
SaveToFile = False
GoTo EndSaveTofile
End If
FileNo = FreeFile
Open strFileName For Binary As #FileNo
ReDim FileData(recset(strField).ActualSize) '重新初始化数组
FileData() = recset(strField).GetChunk(recset(strField).ActualSize) '把OLE字段的内容保存到数组
Put #FileNo, , FileData() '把数组内容保存到文件
Picture1.Picture = LoadPicture(strFileName)
Image1.Picture = Picture1.Picture Close #FileNo
Erase FileData
EndSaveTofile:
recset.Close
Set recset = Nothing
End Function
'-----保存文件到数据库中
'--参数 strFileName 文件路径Public Function SaveFileDB(ByVal strFileName As String) As Boolean
On Error GoTo err
Dim rstTmp As New ADODB.Recordset
Dim cstEam As New ADODB.Stream cstEam.Mode = adModeReadWrite cstEam.Type = adTypeBinary
cstEam.Open
cstEam.LoadFromFile strFileName
strSQL = "select * from fjtable"
rstTmp.Open strSQL, gConn, adOpenDynamic, adLockOptimistic
rstTmp.AddNew
rstTmp!ffile = cstEam.Read()
rstTmp!FTYPE = GetFileType(strFileName)
rstTmp.Update
MsgBox "保存成功!"
rstTmp.Close
Set rstTmp = Nothing
Exit Function
err:
MsgBox err.Number & err.Source & err.Description
End Function'获取文件类型
Public Function GetFileType(ByVal strFileName As String) As String
Dim lngI As Long
Dim EndInt As Long
EndInt = InStr(strFileName, ".") GetFileType = Mid(strFileName, EndInt + 1, Len(strFileName))
End Function
savetofile是将图片读出来再写入指定的文件中,然后显示出来。
SaveFileDB 是将图片转换成二进制再保存到数据库中。
GetFileType 是获取文件类型的.
以下函数已在ACCESS数据库中通过,但SQL server 数据库时就保存不了!!
请大家帮帮忙,如果函数有错,请给个正确的。谢谢!!
Public Function SaveToFile(strTable As String, strField As String, strFilter As String, strFileName As String) As Boolean
'============================================================
' 过程函数名: CommModule.SaveToFile 类型:Function
' 参数:
' strTable (String) :保存图形数据的表名称
' strField (String) :保存图形数据的字段名称
' strFilter (String) :打开表的过滤字符串,用于定位并确保被打开的表的纪录的唯一性
' strFileName (String) :准备保存的图象的文件名称
' 返回:如果保存成功,返回True,如果失败,返回False
'-------------------------------------------------------------
' 说明:把由GetFromFile函数保存到表中OLE字段的数据还原到文件
'-------------------------------------------------------------
' 修订历史:
'=============================================================
Dim recset As ADODB.Recordset, FileData() As Byte, FileNo As Long, FileSize As Long, strSQL As String
strSQL = "Select " & strField & " From " & strTable & " Where " & strFilter & ";"
Set recset = New ADODB.Recordset
recset.Open strSQL, gConn, adOpenDynamic, adLockOptimistic
SaveToFile = True
If recset(strField).Type <> adLongVarWChar Then
SaveToFile = False '如果字段不是OLE字段,返回错误
GoTo EndSaveTofile
End If
If recset.EOF Then '如果记录不存在,返回错误
SaveToFile = False
GoTo EndSaveTofile
End If
FileNo = FreeFile
Open strFileName For Binary As #FileNo
ReDim FileData(recset(strField).ActualSize) '重新初始化数组
FileData() = recset(strField).GetChunk(recset(strField).ActualSize) '把OLE字段的内容保存到数组
Put #FileNo, , FileData() '把数组内容保存到文件
Picture1.Picture = LoadPicture(strFileName)
Image1.Picture = Picture1.Picture Close #FileNo
Erase FileData
EndSaveTofile:
recset.Close
Set recset = Nothing
End Function
'-----保存文件到数据库中
'--参数 strFileName 文件路径Public Function SaveFileDB(ByVal strFileName As String) As Boolean
On Error GoTo err
Dim rstTmp As New ADODB.Recordset
Dim cstEam As New ADODB.Stream cstEam.Mode = adModeReadWrite cstEam.Type = adTypeBinary
cstEam.Open
cstEam.LoadFromFile strFileName
strSQL = "select * from fjtable"
rstTmp.Open strSQL, gConn, adOpenDynamic, adLockOptimistic
rstTmp.AddNew
rstTmp!ffile = cstEam.Read()
rstTmp!FTYPE = GetFileType(strFileName)
rstTmp.Update
MsgBox "保存成功!"
rstTmp.Close
Set rstTmp = Nothing
Exit Function
err:
MsgBox err.Number & err.Source & err.Description
End Function'获取文件类型
Public Function GetFileType(ByVal strFileName As String) As String
Dim lngI As Long
Dim EndInt As Long
EndInt = InStr(strFileName, ".") GetFileType = Mid(strFileName, EndInt + 1, Len(strFileName))
End Function
或者保存图片和取出图片都利用字节数组
ADODB.Stream取图片示例:
Set StmPic = New ADODB.Stream
StrPicTemp = App.Path & "\temp.tmp" '临时文件,用来保存读出的图片
With StmPic
.Type = adTypeBinary
.Open
.Write g_Rs.Fields(8).Value '写入数据库中的数据至Stream中
.SaveToFile StrPicTemp, adSaveCreateOverWrite '将Stream中数据写入临时文件中
.Close
End With
Image1.Picture = LoadPicture(StrPicTemp)
详见:http://blog.csdn.net/online/archive/2004/08/19/78622.aspx
Dim adoSysConn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim StrCnn As String
Dim StrSql As String
Dim RsStream As New ADODB.Stream
Dim StrMyId As String
Private Sub CmmSave_Click()
If txtFilePath.Text = "" Then Exit Sub
StrSql = "Delete from myimge where myid = '" & StrMyId & "'"
adoSysConn.Execute StrSql
StrSql = "Select MyId ,image from Myimge"
If rs.State = adStateOpen Then rs.Close
rs.Open StrSql, adoSysConn, adOpenKeyset, adLockOptimistic
RsStream.Type = adTypeBinary
RsStream.Open
RsStream.LoadFromFile Trim(txtFilePath.Text)
With rs
.AddNew
.Fields("MyId") = StrMyId
.Fields("image") = RsStream.Read
.Update
End With
Set rs = Nothing
Set RsStream = Nothing
Call CboShow
End SubPrivate Sub CmmShow_Click()
Dim PathTmp As String
PathTmp = App.Path & "\Temp.tmp"
StrSql = "Select MyId,image from Myimge where MyId = '" & Trim(cboId.Text) & "' "
If rs.State = adStateOpen Then rs.Close
rs.Open StrSql, adoSysConn, adOpenStatic, adLockReadOnly
RsStream.Type = adTypeBinary
RsStream.Open
RsStream.Write rs!Image
RsStream.SaveToFile PathTmp, adSaveCreateOverWrite
RsStream.Close
Image1.Picture = LoadPicture(PathTmp)
Set rs = Nothing
Set RsStream = Nothing
End SubPrivate Sub CboShow()
StrSql = "select MyId from myimge"
If rs.State = adStateOpen Then rs.Close
rs.Open StrSql, adoSysConn, adOpenKeyset, adLockOptimistic
'If rs.RecordCount < 1 Then Exit Sub
rs.MoveFirst
cboId.Clear
Do While Not rs.EOF
cboId.AddItem rs!MyId
rs.MoveNext
Loop
cboId.ListIndex = 0
End SubPrivate Sub CmmUpdate_Click()
If txtFilePath.Text = "" Then Exit Sub
StrSql = "delete from Myimge where MyId = '" & Trim(cboId.Text) & "'"
adoSysConn.Execute StrSql
StrSql = "Select MyId ,image from Myimge"
If rs.State = adStateOpen Then rs.Close
rs.Open StrSql, adoSysConn, adOpenKeyset, adLockOptimistic
RsStream.Type = adTypeBinary
RsStream.Open
RsStream.LoadFromFile Trim(txtFilePath.Text)
With rs
.AddNew
.Fields("MyId") = Trim(cboId.Text)
.Fields("image") = RsStream.Read
.Update
End With
Set rs = Nothing
Set RsStream = Nothing
Call CboShow
End Sub