用VB6讀寫資料庫中的圖片 (轉)
用VB6讀寫資料庫中的圖片 (轉)[@more@]1,以人名和相關圖片為例說明,為Access,有如下欄位:Name char,picture OLE ,FileLength
Number。當為ms 時,將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_Conn 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 _
"Provr=.Jet.OLEDB.4.0;" & _
"Data =" & db_file & ";" & _
"Persist Security Info=False"
註釋: Get the list of people.
Set rs = m_DBConn.Execute(" Name FROM People ORDER BY Name", , adCmdText)
Do While Not rs.EOF
lstPeople.AddItem rs!Name
rs.MoveNext
L
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
Number。當為ms 時,將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_Conn 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 _
"Provr=.Jet.OLEDB.4.0;" & _
"Data =" & db_file & ";" & _
"Persist Security Info=False"
註釋: Get the list of people.
Set rs = m_DBConn.Execute(" Name FROM People ORDER BY Name", , adCmdText)
Do While Not rs.EOF
lstPeople.AddItem rs!Name
rs.MoveNext
L
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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992019/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 如何限制從資料庫中讀出圖片的大小資料庫
- vb.net 存取資料庫中的圖片 (轉)資料庫
- VB6基本資料庫應用(二):建立資料庫資料庫
- vb向資料庫中讀取單個圖片檔案資料庫
- 瀏覽資料夾中的圖片(用VB實現) (轉)
- java+pgsql實現儲存圖片到資料庫,以及讀取資料庫儲存的圖片JavaSQL資料庫
- 將上傳圖片打上防偽圖片水印並寫入資料庫資料庫
- 從資料庫中的表取幾張圖片,用flash形式動態的顯示圖片資料庫
- 資料庫的讀寫分離資料庫
- vb6 access資料庫當機資料庫
- Python各類影象庫的圖片讀寫方式總結Python
- 資料庫讀寫分離資料庫
- SSIS: 把儲存在資料庫中的圖片匯出來資料庫
- 使用Hibernate和Struts向資料庫中儲存、讀取並顯示圖片資料庫
- delphi 把圖片存入資料庫資料庫
- 資料庫顯示圖片的問題資料庫
- 資料庫中的XML應用例項 (轉)資料庫XML
- C#中查詢資料庫的圖片system.byte[]怎麼轉為imgC#資料庫
- 資料庫中的圖片欄位怎麼在報表中呈現資料庫
- java 獲取圖片屬性、破損圖片處理、寫入日誌、連線資料庫Java資料庫
- 在讀取資料時拼接圖片域名
- 大資料資料庫讀寫分離分庫分表大資料資料庫
- 做資料庫分離讀寫時,sqlServer資料庫資料同步的問題:資料庫SQLServer
- 動態顯示資料庫圖片資料庫
- 從庫中讀圖片顯示到頁面上的主要原始碼原始碼
- 資料庫中的-髒讀,幻讀,不可重複讀資料庫
- 圖資料庫中的“分散式”和“資料切分”(切圖)資料庫分散式
- 從rosbag 中解析出圖片資料ROS
- 為什莫從資料庫中取出的圖片不能顯示出來資料庫
- html中圖片旋轉HTML
- 使用python把圖片存入資料庫Python資料庫
- 醫學影像處理中的資料讀寫
- Java 讀取PDF中的文字和圖片Java
- Python如何讀取pdf中的圖片Python
- Lazarus中對mysql資料庫Blob型別進行讀寫例子MySql資料庫型別
- 資料庫讀寫分離Master-Slave資料庫AST
- Discuz!NT資料庫讀寫分離方案資料庫
- Django 直接使用資料庫連線和遊標讀寫資料庫Django資料庫