求一个用图像框直接绑定access数据库图像字段,将图片写入数据库并读出显示的vb源码!
用image控件或者picture控件直接绑定access数据库图像字段,将图片写入数据库并读出显示这种保存图片到数据库的方法说起来很容易,但是我查阅了至少十八本VB数据库方面书,仿照书中举的例子,没有一个源码能够实现直接把image控件或者picture控件中图片写入数据库并显示的。希望大家能够帮帮忙! --------------------编程问答-------------------- 帮忙顶,希望有人来 --------------------编程问答-------------------- 帮顶。。。 --------------------编程问答-------------------- 用直接的方法估计不行的.你能不能在中间加一层:定义一个DataSet,然后把图片控件绑定到DataSet上.你看可以不?
--------------------编程问答--------------------
--------------------编程问答-------------------- 谢谢大家的帮忙,看来直接将图片直接绑定到数据库是不可行的拉?我先按5楼说的方法试试 --------------------编程问答-------------------- JiangKevin(jxh2003zfr)是用ado做块方法做的,因为我的数据库是用dao生成的,再调用ado,越来越复杂拉 --------------------编程问答-------------------- 看了你另外的贴子http://topic.csdn.net/u/20081113/10/b7453526-307e-4c8d-9894-46a72f3e4bb8.html?seed=1704785625,回答了你的问题,你的方法可以,看看数据库是不是有问题。 --------------------编程问答-------------------- 留言收到。是的,我在很多年前在以前的公司无聊的时候是搞过这么一个小玩意的,当时是用ado+SQL 7.0实现的,picturebox直接绑定到对应字段,addnew或update的时候就loadpicture到picturebox进行增加记录或更新,浏览记录的时候,图片自然会显示。但现在时间长了,代码已不可能寻回,也没有了sql server的环境了。
Private Sub ReadPicFromRs()
Dim sql As String
Dim Chunk() As Byte
Const ChunkSize As Integer = 2384
Dim DataFile As Integer, Chunks, Fragment As Integer
Dim MediaTemp As String
Dim lngTotalSize As Long, lngOffset As Long
Dim ID As Integer
Dim i As Integer
Set adors = New ADODB.Recordset
sql = "select * from aaemployee where lzdate is null and dept='" & CmbDept.Tag & "' and name like '" & LstPerson.Text & "%' order by dept, code" ' where code='" & ID & "'"
adors.Open sql, MLcon.adoSqlCon, adOpenDynamic, adLockOptimistic
If adors.RecordCount = 0 Then Exit Sub
'Read picData to controls from adors
MediaTemp = App.Path & "\files\picturetemp.tmp"
DataFile = 1
Open MediaTemp For Binary Access Write As DataFile
lngTotalSize = adors!Photo.ActualSize
Chunks = lngTotalSize \ ChunkSize
Fragment = lngTotalSize Mod ChunkSize
ReDim Chunk(Fragment)
Chunk() = adors!Photo.GetChunk(Fragment)
Put DataFile, , Chunk()
For i = 1 To Chunks
ReDim Chunk(ChunkSize)
Chunk() = adors!Photo.GetChunk(ChunkSize)
Put DataFile, , Chunk()
Next i
Close DataFile
If MediaTemp = "" Then Exit Sub
Img.Picture = LoadPicture(MediaTemp)
End Sub
Private Sub WritePicToRs()
Dim DataFile As Integer, strPathPicture As String, FileLen As Long
Dim Chunk() As Byte, Chunks, Fragment As Integer, i As Integer
Const ChunkSize As Integer = 2384
adors.AddNew
DataFile = 1
Open strPathPicture For Binary Access Read As DataFile
FileLen = LOF(DataFile)
If FileLen = 0 Then: Close DataFile: adors.Close: Exit Sub
Chunks = FileLen \ ChunkSize
Fragment = FileLen Mod ChunkSize
ReDim Chunk(Fragment)
Get DataFile, , Chunk()
adors!MyPhoto.AppendChunk Chunk()
ReDim Chunk(ChunkSize)
For i = 1 To Chunks
Get DataFile, , Chunk()
adors!MyPhoto.AppendChunk Chunk()
Next i
Close DataFile
End Sub
至于access,由于我的图基本都比较大,所以从一开始就没有考虑access,但我想。。。道理或许是一样的吧,。。。
--------------------编程问答-------------------- 帮你找了一下,也有很多access这方面的介绍啊,例如:
http://topic.csdn.net/t/20020415/16/648581.html
http://topic.csdn.net/u/20080520/10/8c9e603f-b432-469f-a917-adb3a8b68180.html
应该还有很多这方面的介绍吧。。。试试看。。。 --------------------编程问答-------------------- 引用部件Microsoft ActiveX Data Objects *.* Libarary
Dim mycon As New ADODB.Connection
Dim myrest As New ADODB.Recordset
mycon.CursorLocation = adUseClient
mycon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\evershow.mdb;"
mycon.Open
myrest.Open "evershowauto", mycon, adOpenDynamic, adLockOptimistic
If Not IsNull(myrest.Fields("图示")) Then '从数据库中读出图片
Set StmPic = New ADODB.Stream
StrPicTemp = App.Path & "\temp.jpg" '临时文件,用来保存读出的图片
With StmPic
.Type = adTypeBinary
.Open '打开
.Write myrest.Fields("图示") '写入数据库中的二进制文件
.SaveToFile StrPicTemp, adSaveCreateOverWrite
.Close
End With
Image1.Picture = LoadPicture(StrPicTemp) '载入临时文件中的图像
Else
Image1.Picture = LoadPicture()
End If --------------------编程问答-------------------- 上面的是显示图片
这里的是添加图片
工程引用microsoft activex data objects 2.5 library (一定要2.5版以上)
添加部件CommonDialog部件
通过IMAGE控件向ACCESS中添加图片
Dim picpath As String '声明图片路径存储变量
Dim mycon As New ADODB.Connection
Dim myrest As New ADODB.Recordset
mycon.CursorLocation = adUseClient
mycon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\evershow.mdb;"
mycon.Open
Private Sub cmdpicadd_Click() '获取图片路径
cmdal.Flags = cdlOFNFileMustExist
cmdal.Filter = "*.bmp|*.bmp|*.jpg|*.jpg|*.gif|*.gif"
cmdal.FilterIndex = 3
cmdal.ShowOpen
Image1.Picture = LoadPicture(cmdal.FileName)
picpath = cmdal.FileName
End Sub
Private Sub Command1_Click() '移除图片
Image1.Picture = LoadPicture()
End Sub
Private Sub Command7_Click() '添加图片
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.LoadFromFile picpath
Adodc1.Recordset.Fields("图示").Value = mstream.Read
Adodc1.Recordset.Update
End Sub --------------------编程问答-------------------- 不懂,up
补充:VB , 数据库(包含打印,安装,报表)