
'使用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="这是一张图片" >
直接使用企业管理器好像没有办法 *** 作吧,通过软件或自己做个小软件读取。#region//读取数据库中图片到内存.并显示
public void LoadToMemoryAndDisable(string serverAdress, string database)
{
//读取数据库中图片到内存.并显示
SqlConnection conn = new SqlConnection("server=" + serverAdress + "integrated security = sspidatabase = " + database)
SqlCommand cmd = new SqlCommand("select * from imgtable where imgname like '%bmp%'", conn)
conn.Open()
SqlDataReader dr
try
{
dr = cmd.ExecuteReader()
dr.Read()
System.Data.SqlTypes.SqlBinary sb = dr.GetSqlBinary(2)
//或byte[] imageData = (byte[])dr[2]
MemoryStream ms = new MemoryStream(sb.Value)//在内存中 *** 作图片数据
Bitmap bmp = new Bitmap(Bitmap.FromStream(ms))
this.pictureBox1.Image = bmp
dr.Close()
}
catch (Exception ex)
{
MessageBox.Show(ex.Message)
}
finally
{
conn.Close()
}
}
#endregion
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)