Access 数据库中如何存入图片?

Tenner 2004-05-05 01:28:30
我有一字段 Pic (OLE型)用来存放图片,但我用以下语句:
rsRecord("Pic") = Picture1.Picture
出错,告之类型不对
请问应该用什么方法把一幅图片存入 Access 数据库并还能读取到 PictureBox 中?
...全文
75 9 点赞 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dongge2000 2004-05-05
好像是存地址
  • 打赏
  • 举报
回复
online 2004-05-05
*************************************************************************
'**
'** 使用 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:\My Documents\客户资料1.mdb"

'读取文件到内容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二进制模式
.Open
.LoadFromFile "c:\test.doc"
End With

'打开保存文件的表
Set iRe = New ADODB.Recordset
With iRe
.Open "表", iConc, adOpenKeyset, adLockOptimistic
.AddNew '新增一条记录
.Fields("保存文件内容的字段") = 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=\\xz\c$\Inetpub\zj\zj\zj.mdb"

'打开表
Set iRe = New ADODB.Recordset
iRe.Open "tb_img", iConc, adOpenKeyset, adLockReadOnly
iRe.Filter = "id=64"

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

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

  • 打赏
  • 举报
回复
online 2004-05-05
可以用 ADODB.Stream来实现:

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim stm As ADODB.Stream

Private Sub SavePictureToDB(cn As ADODB.Connection)
'将BMP图片存入数据库
On Error GoTo EH
Set stm = New ADODB.Stream
rs.Open "select ImagePath,ImageValue from tbl_Image", cn, adOpenKeyset, adLockOptimistic
CommonDialog1.ShowOpen
Text1.Text = CommonDialog1.FileName

With stm
.Type = adTypeBinary
.Open
.LoadFromFile CommonDialog1.FileName
End With
With rs
.AddNew
.Fields("ImagePath") = Text1.Text
.Fields("ImageValue") = stm.Read
.Update
End With
rs.Close
Set rs = Nothing
Exit Sub
EH: MsgBox Err.Description, vbInformation, "Error"
End Sub


Private Sub LoadPictureFromDB(cn As ADODB.Connection)
'载数据库中读出BMP图片
On Error GoTo EH
Dim strTemp As String
Set stm = New ADODB.Stream
strTemp = "c:\temp.tmp" '临时文件,用来保存读出的图片
rs.Open "select ImagePath,ImageValue from tbl_image", cn, , , adCmdText
With stm
.Type = adTypeBinary
.Open
.Write rs("ImageValue")
.SaveToFile strTemp, adSaveCreateOverWrite
.Close
End With
Image1.Picture = LoadPicture(strTemp)
Set stm = Nothing
rs.Close
Set rs = Nothing
Exit Sub
EH: MsgBox Err.Description, vbInformation, "Error"
End Sub

  • 打赏
  • 举报
回复
flc 2004-05-05
关注
  • 打赏
  • 举报
回复
sdmyhy 2004-05-05
想要例程吗,要的话给我写信,sdmyhy@21cn.com
  • 打赏
  • 举报
回复
stefli 2004-05-05
先的引用Microsoft ActiveX DATA OBJECTS 中的一个~
然后,其实和上面一样,呵呵, 多个选择~


Public FileName As String


Private Sub Command2_Click()
CommonDialog1.ShowOpen
FileName = CommonDialog1.FileName
Call UpLoadPic
End Sub

Private Sub UpLoadPic()
On Error Resume Next
Dim stm As ADODB.Stream
Set stm = New ADODB.Stream
stm.Mode = adModeReadWrite
stm.Type = adTypeBinary
stm.Open
stm.LoadFromFile FileName
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("image").Value = stm.Read()
Adodc1.Recordset.Update
Adodc1.Refresh
stm.Close
Adodc1.Recordset.Close
If Err.Number > 0 Then
MsgBox Err.Description
End If
End Sub

Private Sub Form_Load()
Set Picture1.DataSource = Adodc1.Recordset
Picture1.DataField = "image"
End Sub
  • 打赏
  • 举报
回复
pcwak 2004-05-05
我一般是设置
Picture1.DataSource
Picture1.DataField
  • 打赏
  • 举报
回复
Tenner 2004-05-05
那从数据库中读取时又如何将它读入PictureBox呢?
  • 打赏
  • 举报
回复
pcwak 2004-05-05
Private Sub InsPic()
On Error GoTo ervs
Set Stm = New ADODB.Stream
ErrMsg = True
Stm.Mode = adModeReadWrite
Stm.Type = adTypeBinary
Set Rs = Nothing
Set Rs = New ADODB.Recordset
Stm.Open
Stm.LoadFromFile FileName
Rs.AddNew
Rs.Fields("pict").Value = Stm.Read()
Rs.Update
ErrMsg = False
Stm.Close
Rs.Close
ervs:
If ErrMsg Then
Rs.Close
Stm.Close
MsgBox Err.Description
End If
End If
End Sub

  • 打赏
  • 举报
回复
相关推荐
发帖
VB基础类
加入

7547

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2004-05-05 01:28
社区公告
暂无公告