如何在VB中将图片存入数据库,并且从数据库读出显示到界面上
4个回答
展开全部
首先你没有说明是什么数据库
我先以access数据库为例:
数据库:
info表
name段,photo段(设置为ole对象)
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对象
Public Function SaveFileToDB(ByVal Filename As String, dbField As ADODB.Field, Optional PackageSize As Long = 8192&) As Long
On Error GoTo errHandle
Dim lngActualSize As Long, lngCurrentSize As Long
Dim bTemp() As Byte
Dim hFile As Long
Dim i As Integer
Dim lChunkCount As Long
Dim lChunkRemainder As Long
lngActualSize = FileLen(Filename)
lngCurrentSize = 0
If lngActualSize <= 0 Then
Err.Raise ERR_SIZE_EQU_OR_LESS_ZERO, "writeFileToDB"
End If
'读取文件内容
hFile = FreeFile()
Open Filename For Binary As hFile
ReDim bTemp(PackageSize) '临时存贮块
lChunkCount = LOF(hFile) \ PackageSize '取块数
lChunkRemainder = LOF(hFile) Mod PackageSize
If lChunkRemainder > 0 Then lChunkCount = lChunkCount + 1
For i = 1 To lChunkCount
Get hFile, , bTemp '从文件中取出一块
dbField.AppendChunk (bTemp) '将块写入字段中
Next
Close #1
SaveFileToDB = 0
Exit Function
errHandle:
SaveFileToDB = Err.Number
End Function
Public Function GetFileFromDB(dbField As ADODB.Field, vdata As Variant, Optional PackageSize As Long = 8192&) As Long
' Optional ByVal SaveAs As String = "",
'如果SaveAs为空串则保存在vData这个变体类型中,否则保存在文件中
On Error GoTo errHandle
Dim i%, lngActualSize As Long, lChunkCount As Long
Dim vTemp As Variant, bTemp() As Byte
Dim hFile As Long
lngActualSize = dbField.ActualSize
If lngActualSize <= 0 Then
Err.Raise ERR_SIZE_EQU_OR_LESS_ZERO, "getFileFromDB"
End If
lChunkCount = lngActualSize \ PackageSize
If (lngActualSize Mod PackageSize <> 0) Then
lChunkCount = lChunkCount + 1
End If
hFile = FreeFile()
'Open "" & App.Path & "" & " \Temp.Temp" For Binary As hFile
Open "" & App.Path & "" & "\Temp.Temp" For Binary As hFile
For i = 1 To lChunkCount
bTemp = dbField.GetChunk(PackageSize)
Put #hFile, , bTemp
Next
Close
GetFileFromDB = 0
Exit Function
errHandle:
GetFileFromDB = Err.Number
End Function
Private Sub cmdGet_Click()
'重新打开记录集,刷新记录
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 Info Where Name='" & UserNames.Text & "'", ADOCon, adOpenDynamic, adLockOptimistic
'打开一个ADO动态集 表名为table
If ADORst.EOF = False Then
Else
MsgBox "没有这条记录", vbCritical, "提示:"
Exit Sub
End If
Set Adodc1.Recordset = ADORst '将动态集赋给ADO控件
Set ADOFld = ADORst("Photo") '给ADODB.Field对象赋值
Call GetFileFromDB(ADOFld, vdata) '调用字程序,给第三个字段(image)赋值
Image1.Picture = LoadPicture("" & App.Path & "" & "\Temp.Temp") '从临时文件加载图片
Kill ("" & App.Path & "" & "\Temp.Temp") '删除临时文件
End Sub
Private Sub cmdPreView_Click()
'显示打开文件的公用对话框,选择需要加入数据库的图片
CommonDialog1.Filter = "图片文件|*.bmp;*.ico;*.jpg;*.gif;*.jpeg"
CommonDialog1.ShowOpen
Filename = CommonDialog1.Filename
Text1.Text = CommonDialog1.Filename
Image0.Picture = LoadPicture(Filename) '预览图片
End Sub
Private Sub cmdSave_Click()
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 Info Where Name='" & txtAddName.Text & "'", ADOCon, adOpenDynamic, adLockOptimistic
'打开一个ADO动态集 表名为table
Set Adodc1.Recordset = ADORst '将动态集赋给ADO控件
If ADORst.EOF = True Then
ADORst.AddNew '新增记录
ADORst("Name").Value = txtAddName.Text '给动态集的第一个字段赋值
'ADORst("ID").Value = txtAddID.Text '给动态集的第二个字段赋值
Set ADOFld = ADORst("Photo") '给ADODB.Field对象赋值
Call SaveFileToDB(Filename, ADOFld) '调用字程序,给第三个字段(image)赋值
ADORst.Update
Else
MsgBox "已有此条记录", vbCritical, "提示:"
End If
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 Info", ADOCon, adOpenDynamic, adLockOptimistic
Set Adodc1.Recordet = ADORst
End Sub
Private Sub Form_Unload(Cancel As Integer)
'记得关闭打开的数据库,释放资源
Set ADORst = Nothing
Set ADOCon = Nothing
End Sub
我先以access数据库为例:
数据库:
info表
name段,photo段(设置为ole对象)
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对象
Public Function SaveFileToDB(ByVal Filename As String, dbField As ADODB.Field, Optional PackageSize As Long = 8192&) As Long
On Error GoTo errHandle
Dim lngActualSize As Long, lngCurrentSize As Long
Dim bTemp() As Byte
Dim hFile As Long
Dim i As Integer
Dim lChunkCount As Long
Dim lChunkRemainder As Long
lngActualSize = FileLen(Filename)
lngCurrentSize = 0
If lngActualSize <= 0 Then
Err.Raise ERR_SIZE_EQU_OR_LESS_ZERO, "writeFileToDB"
End If
'读取文件内容
hFile = FreeFile()
Open Filename For Binary As hFile
ReDim bTemp(PackageSize) '临时存贮块
lChunkCount = LOF(hFile) \ PackageSize '取块数
lChunkRemainder = LOF(hFile) Mod PackageSize
If lChunkRemainder > 0 Then lChunkCount = lChunkCount + 1
For i = 1 To lChunkCount
Get hFile, , bTemp '从文件中取出一块
dbField.AppendChunk (bTemp) '将块写入字段中
Next
Close #1
SaveFileToDB = 0
Exit Function
errHandle:
SaveFileToDB = Err.Number
End Function
Public Function GetFileFromDB(dbField As ADODB.Field, vdata As Variant, Optional PackageSize As Long = 8192&) As Long
' Optional ByVal SaveAs As String = "",
'如果SaveAs为空串则保存在vData这个变体类型中,否则保存在文件中
On Error GoTo errHandle
Dim i%, lngActualSize As Long, lChunkCount As Long
Dim vTemp As Variant, bTemp() As Byte
Dim hFile As Long
lngActualSize = dbField.ActualSize
If lngActualSize <= 0 Then
Err.Raise ERR_SIZE_EQU_OR_LESS_ZERO, "getFileFromDB"
End If
lChunkCount = lngActualSize \ PackageSize
If (lngActualSize Mod PackageSize <> 0) Then
lChunkCount = lChunkCount + 1
End If
hFile = FreeFile()
'Open "" & App.Path & "" & " \Temp.Temp" For Binary As hFile
Open "" & App.Path & "" & "\Temp.Temp" For Binary As hFile
For i = 1 To lChunkCount
bTemp = dbField.GetChunk(PackageSize)
Put #hFile, , bTemp
Next
Close
GetFileFromDB = 0
Exit Function
errHandle:
GetFileFromDB = Err.Number
End Function
Private Sub cmdGet_Click()
'重新打开记录集,刷新记录
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 Info Where Name='" & UserNames.Text & "'", ADOCon, adOpenDynamic, adLockOptimistic
'打开一个ADO动态集 表名为table
If ADORst.EOF = False Then
Else
MsgBox "没有这条记录", vbCritical, "提示:"
Exit Sub
End If
Set Adodc1.Recordset = ADORst '将动态集赋给ADO控件
Set ADOFld = ADORst("Photo") '给ADODB.Field对象赋值
Call GetFileFromDB(ADOFld, vdata) '调用字程序,给第三个字段(image)赋值
Image1.Picture = LoadPicture("" & App.Path & "" & "\Temp.Temp") '从临时文件加载图片
Kill ("" & App.Path & "" & "\Temp.Temp") '删除临时文件
End Sub
Private Sub cmdPreView_Click()
'显示打开文件的公用对话框,选择需要加入数据库的图片
CommonDialog1.Filter = "图片文件|*.bmp;*.ico;*.jpg;*.gif;*.jpeg"
CommonDialog1.ShowOpen
Filename = CommonDialog1.Filename
Text1.Text = CommonDialog1.Filename
Image0.Picture = LoadPicture(Filename) '预览图片
End Sub
Private Sub cmdSave_Click()
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 Info Where Name='" & txtAddName.Text & "'", ADOCon, adOpenDynamic, adLockOptimistic
'打开一个ADO动态集 表名为table
Set Adodc1.Recordset = ADORst '将动态集赋给ADO控件
If ADORst.EOF = True Then
ADORst.AddNew '新增记录
ADORst("Name").Value = txtAddName.Text '给动态集的第一个字段赋值
'ADORst("ID").Value = txtAddID.Text '给动态集的第二个字段赋值
Set ADOFld = ADORst("Photo") '给ADODB.Field对象赋值
Call SaveFileToDB(Filename, ADOFld) '调用字程序,给第三个字段(image)赋值
ADORst.Update
Else
MsgBox "已有此条记录", vbCritical, "提示:"
End If
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 Info", ADOCon, adOpenDynamic, adLockOptimistic
Set Adodc1.Recordet = ADORst
End Sub
Private Sub Form_Unload(Cancel As Integer)
'记得关闭打开的数据库,释放资源
Set ADORst = Nothing
Set ADOCon = Nothing
End Sub
展开全部
数据库中表的字段有一种叫备注型或SQL Server好像有二进制类型的字段的,图片可采用二进制方式存取。
但建议您不要这么做
您可以讲文件的路径存入数据库,比较合适
然后用picturebox加载路径下的图片
要是图片不在本地,您可以使用异步加载的方式,读取远程图片
但建议您不要这么做
您可以讲文件的路径存入数据库,比较合适
然后用picturebox加载路径下的图片
要是图片不在本地,您可以使用异步加载的方式,读取远程图片
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用open把图片变成二进制数据Briny,. 存在一个变量里面,然后将变量里的东东传到数据库~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你说的应该是数据引擎吧
数据库的种类不是哪个语言分的
常见的关系数据库有sql
server,
oracle,access,my
sql,
db2,dbf等了
非关系数据库现在比较多的是结构化数据库
最常见就是xml数据库(思想跟xml文件差不多
但是xml文件区别于xml数据库
不是一个概念)
如果你问的是数据引擎了
vb一般就是dao
rdo
ado吧?
数据库的种类不是哪个语言分的
常见的关系数据库有sql
server,
oracle,access,my
sql,
db2,dbf等了
非关系数据库现在比较多的是结构化数据库
最常见就是xml数据库(思想跟xml文件差不多
但是xml文件区别于xml数据库
不是一个概念)
如果你问的是数据引擎了
vb一般就是dao
rdo
ado吧?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询