如何VB6编程实现 Access数据库中存取图像文件???

shipL 2004-10-09 11:54:17
如题 图像文件(*.jpg *.bmp *.gif 等)
ggjjddmm指点一下呀!最好有例子!多谢了,急用!
...全文
138 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Option Explicit

Dim cn As New ADODB.Connection, rs As New ADODB.Recordset

'保存
Private Sub Command1_Click()
Dim bteContent() As Byte

Open "C:\aa.bmp" For Binary Access Read As #1
bteContent = InputB(LOF(1), #1)
Close #1

If rs.State = adStateOpen Then rs.Close
rs.Open "select * from tablename", cn, adOpenDynamic, adLockPessimistic
rs.AddNew
rs!Name = "张三"
rs!AGE = 22
rs!SEX = "男"
rs.Fields("PHOTO").AppendChunk bteContent
rs.Update

Erase bteContent
End Sub

'打开
Private Sub Command2_Click()
Dim bteContent() As Byte
If rs.State = adStateOpen Then rs.Close
rs.Open "select * from tablename", cn, adOpenForwardOnly, adLockReadOnly
bteContent = rs.Fields("PHOTO").GetChunk(rs.Fields("PHOTO").ActualSize)

Open "C:\aa.bmp" For Binary Access Write As #1
Put #1, , bteContent
Close #1
Image1.Picture = LoadPicture("C:\aa.bmp")
End Sub

Private Sub Form_Load()
On Error GoTo Errhandle
cn.ConnectionString = "Driver={SQL Server};SERVER=DataServer;DATABASE=zxzx;UID=information;PWD=information*&#"
cn.Open

Exit Sub
Errhandle:
MsgBox Err.Description, vbExclamation
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error Resume Next
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

回复
'http://support.microsoft.com/default.aspx?scid=kb;EN-US;258038

'*************************************************************************
'**
'** 使用 ADODB.Stream 保存/读取文件到数据库
'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
'**
'** ----- 数据库连接字符串模板 ---------------------------------------
'** ACCESS数据库
'** iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
'** ";Data Source=数据库名"
'**
'** SQL数据库
'** iConcStr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
'** "User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名"
'**

'保存文件到数据库中
Sub s_SaveFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConcStr As String

'数据库连接字符串
iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=F:\csdn_vb\database\保存图片\img.mdb"
Dim iConc As ADODB.Connection
Set iConc = New ADODB.Connection

iConc.Open iConcStr
'读取文件到内容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二进制模式
.Open
.LoadFromFile App.Path + "\com.doc"
End With

'打开保存文件的表
Set iRe = New ADODB.Recordset
With iRe
.Open "select * from img", iConc, 1, 3
.AddNew '新增一条记录
.Fields("photo") = iStm.Read
.Update
End With

'完成后关闭对象
iRe.Close
iStm.Close
End Sub

Sub s_ReadFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConc As String

'数据库连接字符串
iConc = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=F:\csdn_vb\database\保存图片\img.mdb"

'打开表
Set iRe = New ADODB.Recordset
iRe.Open "img", iConc, adOpenKeyset, adLockReadOnly
iRe.Filter = "id=3" '比如单击datagrid后找到id=3的纪录

'保存到文件
Set iStm = New ADODB.Stream
With iStm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write iRe("photo")
.SaveToFile "c:\com.doc"
End With

OLE1.SourceDoc = "c:\\com.doc"
OLE1.Action = 1 'use the old action method
OLE1.SizeMode = vbOLESizeAuttoSize '调节对象大小使其正好充满OLE容器控件

'Picture1.Picture = LoadPicture("c:\tesd.bmp")

'关闭对象
iRe.Close
iStm.Close
End Sub



Private Sub Command1_Click()
Call s_ReadFile
End Sub

Private Sub Command2_Click()
Call s_SaveFile
End Sub


回复
使用Stream对象,可以实现对数据库的图像存取。
数据库中存放图像的字段是二进制类型(Access为OLE类型)。
比如,如果用“CommonDialog”控件来选择你硬盘上的图像文件;
用“Picture”控件来显示图像,那么下面的代码供参考:
(已连接数据库,打开了相应的记录集rs)
Dim StmPic As ADODB.Stream
Dim StrPicTemp As String
  ......
  '保存你所选择的图像
  Set StmPic = New ADODB.Stream
StmPic.Type = adTypeBinary '指定流是二进制类型
StmPic.Open           '将数据获取到Stream对象中
StmPic.LoadFromFile (CommonDialog1.FileName) '将选择的图像加载到打开的StmPic中
rs.AddNew
rs.Fields(1).Value = StmPic.Read '从StmPic对象中读取数据
rs.Update
StmPic.Close
  ......
  '读取显示数据库中的图像
  Set StmPic = New ADODB.Stream
StrPicTemp = "c:\temp.tmp" '临时文件,用来保存读出的图片
With StmPic
    .Type = adTypeBinary
    .Open
    .Write rs.Fields(1) '写入数据库中的数据至Stream中
    .SaveToFile StrPicTemp, adSaveCreateOverWrite '将Stream中数据写入临时文件中
    .Close
  End With
  Picture1.Picture = LoadPicture(StrPicTemp) '用Picture控件显示图像
回复
shipL 2004-10-09
多谢各位的帮助!小弟感激不尽!
马上试过,过会结贴!
回复
jam021 2004-10-09
关注,帮你顶!
回复
发动态
发帖子

1180

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
社区公告
暂无公告