| 欄位名 | 資料類型 | 說明 |
|---|---|---|
| Name | Char | 人名 |
| Picture | OLE Object (Access) / Image (SQL Server) | 二進位圖片資料 |
| FileLength | Number | 檔案大小(選填) |
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 Const BLOCK_SIZE = 10000 ' 分塊讀寫大小
Private Sub Form_Load()
Dim db_file As String, rs As ADODB.Recordset
db_file = App.Path & "\dbpict.mdb"
Set m_DBConn = New ADODB.Connection
m_DBConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db_file
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
End Sub
Private Sub lstPeople_Click()
Dim rs As ADODB.Recordset, bytes() As Byte, file_name As String
Set rs = m_DBConn.Execute("SELECT Picture, FileLength FROM People WHERE Name='" & lstPeople.Text & "'")
If Not rs.EOF Then
bytes = rs!Picture ' 直接取得二進位陣列
file_name = TemporaryFileName
Open file_name For Binary Access Write As #1
Put #1, , bytes
Close #1
picPhoto.Picture = LoadPicture(file_name)
Kill file_name
End If
rs.Close
End Sub
Private Sub cmdSave_Click()
Dim bytes() As Byte, file_name As String
If CommonDialog1.ShowOpen Then
file_name = CommonDialog1.FileName
Open file_name For Binary Access Read As #1
ReDim bytes(LOF(1) - 1)
Get #1, , bytes
Close #1
m_DBConn.Execute "UPDATE People SET Picture = ?, FileLength = ? WHERE Name = '" & lstPeople.Text & "'", _
Array(bytes, UBound(bytes) + 1), adCmdText
End If
End Sub
將 Picture 欄位類型改為 image(或 SQL 2005+ 的 varbinary(max)),連接字串改用 SQLOLEDB,其餘邏輯不變。