何用ADO和access的数据库存储和显示图片

dragonloveboy 2006-03-02 02:59:39
哥哥姐姐帮帮忙:
谁能告诉我何用ADO和access的数据库存储和显示图片 ,特别是用ADO显示图片(我不知道能不能实现)
谢谢!
...全文
195 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZOU_SEAFARER 2006-03-02
  • 打赏
  • 举报
回复

看看这个合适不?






1,以人名和相关图片为例说明,数据库为Access,有如下字段:Name char,picture OLE object,FileLength

Number。当为ms sql时,将picture改为lob即可。

2,示例包含control:commom dialog,picture,listbox。

源码如下:

Option Explicit



Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As

String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long,

ByVal lpBuffer As String) As Long

Private Const MAX_PATH = 260



Private m_DBConn As ADODB.Connection



Private Const BLOCK_SIZE = 10000

注释: Return a temporary file name.

Private Function TemporaryFileName() As String

Dim temp_path As String

Dim temp_file As String

Dim length As Long



注释: Get the temporary file path.

temp_path = Space$(MAX_PATH)

length = GetTempPath(MAX_PATH, temp_path)

temp_path = Left$(temp_path, length)



注释: Get the file name.

temp_file = Space$(MAX_PATH)

GetTempFileName temp_path, "per", 0, temp_file

TemporaryFileName = Left$(temp_file, InStr(temp_file, Chr$(0)) - 1)

End Function

Private Sub Form_Load()

Dim db_file As String

Dim rs As ADODB.Recordset



注释: Get the database file name.

db_file = App.Path

If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"

db_file = db_file & "dbpict.mdb"



注释: Open the database connection.

Set m_DBConn = New ADODB.Connection

m_DBConn.Open _

"Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=" & db_file & ";" & _

"Persist Security Info=False"



注释: Get the list of people.

Set rs = m_DBConn.Execute("SELECT Name FROM People ORDER BY Name", , adCmdText)

Do While Not rs.EOF

lstPeople.AddItem rs!Name

rs.MoveNext

Loop



rs.Close

Set rs = Nothing

End Sub

Private Sub Form_Resize()

lstPeople.Height = ScaleHeight

End Sub





注释: Display the clicked person.

Private Sub lstPeople_Click()

Dim rs As ADODB.Recordset

Dim bytes() As Byte

Dim file_name As String

Dim file_num As Integer

Dim file_length As Long

Dim num_blocks As Long

Dim left_over As Long

Dim block_num As Long

Dim hgt As Single



picPerson.Visible = False

Screen.MousePointer = vbHourglass

DoEvents



注释: Get the record.

Set rs = m_DBConn.Execute("SELECT * FROM People WHERE Name=注释:" & _

lstPeople.Text & "注释:", , adCmdText)

If rs.EOF Then Exit Sub



注释: Get a temporary file name.

file_name = TemporaryFileName()



注释: Open the file.

file_num = FreeFile

Open file_name For Binary As #file_num



注释: Copy the data into the file.

file_length = rs!FileLength

num_blocks = file_length / BLOCK_SIZE

left_over = file_length Mod BLOCK_SIZE



For block_num = 1 To num_blocks

bytes() = rs!Picture.GetChunk(BLOCK_SIZE)

Put #file_num, , bytes()

Next block_num



If left_over > 0 Then

bytes() = rs!Picture.GetChunk(left_over)

Put #file_num, , bytes()

End If



Close #file_num



注释: Display the picture file.

picPerson.Picture = LoadPicture(file_name)

picPerson.Visible = True



Width = picPerson.Left + picPerson.Width + Width - ScaleWidth

hgt = picPerson.Top + picPerson.Height + Height - ScaleHeight

If hgt < 1440 Then hgt = 1440

Height = hgt



Kill file_name

Screen.MousePointer = vbDefault

End Sub



Private Sub mnuRecordAdd_Click()

Dim rs As ADODB.Recordset

Dim person_name As String

Dim file_num As String

Dim file_length As String

Dim bytes() As Byte

Dim num_blocks As Long

Dim left_over As Long

Dim block_num As Long



person_name = InputBox("Name")

If Len(person_name) = 0 Then Exit Sub



dlgPicture.Flags = _

cdlOFNFileMustExist Or _

cdlOFNHideReadOnly Or _

cdlOFNExplorer

dlgPicture.CancelError = True

dlgPicture.Filter = "Graphics Files|*.bmp;*.ico;*.jpg;*.gif"



On Error Resume Next

dlgPicture.ShowOpen

If Err.Number = cdlCancel Then

Exit Sub

ElseIf Err.Number <> 0 Then

MsgBox "Error " & Format$(Err.Number) & _

" selecting file." & vbCrLf & Err.Description

Exit Sub

End If



注释: Open the picture file.

file_num = FreeFile

Open dlgPicture.FileName For Binary Access Read As #file_num



file_length = LOF(file_num)

If file_length > 0 Then

num_blocks = file_length / BLOCK_SIZE

left_over = file_length Mod BLOCK_SIZE



Set rs = New ADODB.Recordset

rs.CursorType = adOpenKeyset

rs.LockType = adLockOptimistic

rs.Open "Select Name, Picture, FileLength FROM People", m_DBConn



rs.AddNew

rs!Name = person_name

rs!FileLength = file_length



ReDim bytes(BLOCK_SIZE)

For block_num = 1 To num_blocks

Get #file_num, , bytes()

rs!Picture.AppendChunk bytes()

Next block_num



If left_over > 0 Then

ReDim bytes(left_over)

Get #file_num, , bytes()

rs!Picture.AppendChunk bytes()

End If



rs.Update

Close #file_num



lstPeople.AddItem person_name

lstPeople.Text = person_name

End If

End Sub
faysky2 2006-03-02
  • 打赏
  • 举报
回复
同上,用ADO的流来做,读取图片时,先把图片读到临时文件,在显示出来
饮水需思源 2006-03-02
  • 打赏
  • 举报
回复
使用流对象保存和显示图片
打开vb6,新建工程。

添加两个按钮,一个image控件
注意:Access中的photo字段类型为OLE对象.
SqlServer中的photo字段类型为Image

'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
‘2.5版本以下不支持Stream对象
Dim iConcstr As String
Dim iConc As ADODB.Connection


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

'读取文件到内容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二进制模式
.Open
.LoadFromFile App.Path + "\test.jpg"
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
'打开表
Set iRe = New ADODB.Recordset
‘得到最新添加的纪录
iRe.Open "select top 1 * from img order by id desc", iConc, adOpenKeyset, adLockReadOnly
'保存到文件
Set iStm = New ADODB.Stream
With iStm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write iRe("photo")
‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误.
.SaveToFile App.Path & "\test1.jpg"
End With


Image1.Picture = LoadPicture(App.Path & "\test1.jpg")
'关闭对象
iRe.Close
iStm.Close
End Sub


Private Sub Command1_Click()
Call s_ReadFile
End Sub


Private Sub Command2_Click()
Call s_SaveFile
End Sub


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

‘下面的语句是连接sqlserver数据库的.
‘iConcstr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
‘ "User ID=sa;Password=;Initial Catalog=test;Data Source=yang"


Set iConc = New ADODB.Connection
iConc.Open iConcstr
End Sub


Private Sub Form_Unload(Cancel As Integer)
iConc.Close
Set iConc = Nothing
End Sub

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧