如何才能往数据库里读取图片数据或者从数据库里读图片

如何才能往数据库里读取图片数据或者从数据库里读图片,第1张

给你提供个ACCESS版的VB代码,使用时调用这些过程即可:

'使用ADODB.Stream来保存/读取图像文件到数据

'引用Microsoft ActiveX Data Objects 2.5 Library及以上版本

'保存文件到数据库中

Sub SaveFile()

Dim Stm As New ADODB.Stream

Dim Cnn As New ADODB.Connection

Dim rs As New ADODB.Recordset

Dim strCnn As String

strCnn = "Provider=Microsoft.Jet.OLEDB.4.0Persist Security Info=FalseData Source=" &_

App.Path &"\DB1.mdb"

Cnn.Open strCnn

'读取文件到内存(二进制模式)

With Stm

.Type = adTypeBinary

.Open

.LoadFromFile App.Path + "\Image1.bmp"

End With

With rs

.Open "SELECT * FROM TABLE1", Cnn, 1, 3

.AddNew

.Fields("IMAGE") = Stm.Read

.Update

End With

rs.Close

Stm.Close

Set rs = Nothing

Set Cnn = Nothing

Set Stm = Nothing

End Sub

'从数据库中读取图像文件

Sub ReadFile()

Dim Stm As New ADODB.Stream

Dim Cnn As New ADODB.Connection

Dim rs As New ADODB.Recordset

Dim strCnn As String

strCnn = "Provider=Microsoft.Jet.OLEDB.4.0Persist Security Info=FalseData Source=" &_

App.Path &"\DB1.mdb"

Cnn.Open strCnn

rs.Open "SELECT IMAGE FROM TABLE1 WHERE ID = 18", Cnn, adOpenKeyset, adLockReadOnly

'保存到文件

With Stm

.Mode = adModeReadWrite

.Type = adTypeBinary

.Open

.Write rs("IMAGE")

.SaveToFile App.Path + "\Image2.bmp"

End With

'显示图片

Picture1.Picture = LoadPicture(App.Path + "\Image2.bmp")

rs.Close

Stm.Close

Set rs = Nothing

Set Cnn = Nothing

Set Stm = Nothing

End Sub

确保你的图片已经保存到数据库,如果没什么错误,那就看下面

showming.asp

<!--#include file="../conn/conn1.asp" -->'连接数据库

<%

id=clng(trim(request("id")))

if id="" then response.End

response.Expires=0

response.buffer=true

response.Clear()

set rs=server.CreateObject("adodb.recordset")

sql="select * from product where productid="&id&""

rs.open sql,conn,3,1

response.ContentType="image/*"

response.BinaryWrite rs("photo")

rs.close

set rs=nothing

conn.close

set conn=nothing

%>

显示的图片的页面:picshow.asp

<img src="showimg.asp?id=<%=rs("productid")%>" width="400" height="300" border="0" alt="这是一张图片" >


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6842818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-29
下一篇 2023-03-29

发表评论

登录后才能评论

评论列表(0条)

保存