文章編號: 103257 - 上次校閱: 2007年1月18日 - 版次: 2.3

ACC: 讀取儲存,寫入二進位大型物件 (BLOB) (& I)

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
進階: 須具備專家編碼、 互通性,與多使用者技能。

全部展開 | 全部摺疊

結論

您可以儲存在欄位的大型資料物件 (例如音效、 視訊,或圖形的資料) 與 OLE 物件資料型別,Microsoft Access 表格中。無法表示某些大型的二進位資料物件,但是,如果他們不能瞭解所儲存的資料 「 OLE 伺服器。您也可以將可執行程式檔案的複本或其他非 OLE 資料儲存在 Microsoft Access 資料表。這種類型的資料被指二進位大型物件點陣圖 (BLOB)。

本文假設您已熟悉使用 Visual Basic 應用程式以及建立 Microsoft Access 應用程式使用程式設計與 Microsoft Access 所提供的工具。如需有關 Visual Basic 應用程式,請參閱您的建置應用程式與 Microsoft 存取] 手冊版本。

注意: 應用程式的 Visual Basic 稱為存取基本在 Microsoft Access 版本 1.x 和 2.0 版。如需有關存取基本的詳細資訊,請參閱 「 簡介來發展"手冊,在 Microsoft Access 版本 1.x 或 < 建置應用程式 」 手動在 Microsoft Access 版本 2.0。

其他相關資訊

下列範例包含兩個範例使用者定義函式,可以讓您管理大量的二進位資料欄位中使用 OLE Object 資料型別。使用者定義函式是 ReadBLOB() 和 WriteBLOB()。

  • ReadBLOB() 函式會讀取二進位磁碟檔案,並將之儲存在 「 OLE 物件 」 欄位中。
  • WriteBLOB() 函式寫入二進位資料儲存在 OLE 物件欄位到磁碟檔案。
本範例示範如何將二進位檔案複製到 「 OLE 物件 」 欄位,以及然後如何將它寫回至新的磁碟檔案:

  1. 建立新的模組呼叫 BLOB,並在模組的宣告區段中輸入下列行:
    外顯 選項
    const 區塊大小 = 32768
    注意: 如果您使用 Microsoft Access 2.0,您必須將兩個先前的後加上下列定義:
          Const dbOpenTable = DB_OPEN_TABLE
          Const acSysCmdInitMeter = SYSCMD_INITMETER
          Const acSysCmdUpdateMeter = SYSCMD_UPDATEMETER
          Const acSysCmdRemoveMeter = SYSCMD_REMOVEMETER
  2. 請輸入下列程式碼模組中。

    注意: 在下列範例程式碼的線條結尾是以底線 (_) 是用來當做行接續字元。重新建立這個程式碼中存取基本時,則請移除行尾底線。

    注意: 對於 Microsoft Access 1.x 無法使用下列的技巧。 對於 Microsoft Access 1.x 您必須修改程式碼,讓它使用資料表變數,而不是資料錄集變數和 Opentable 巨集函式,而不是 OpenRecordset。
          '**************************************************************
          ' FUNCTION: ReadBLOB()
          '
          ' PURPOSE:
          '   Reads a BLOB from a disk file and stores the contents in the
          '   specified table and field.
          '
          ' PREREQUISITES:
          '   The specified table with the OLE object field to contain the
          '   binary data must be opened in Visual Basic code (Access Basic
          '   code in Microsoft Access 2.0 and earlier) and the correct record
          '   navigated to prior to calling the ReadBLOB() function.
          '
          ' ARGUMENTS:
          '   Source - The path and filename of the binary information
          '            to be read and stored.
          '   T      - The table object to store the data in.
          '   Field  - The OLE object field in table T to store the data in.
          '
          ' RETURN:
          '   The number of bytes read from the Source file.
          '**************************************************************
          Function ReadBLOB(Source As String, T As Recordset, _
          sField As String)
              Dim NumBlocks As Integer, SourceFile As Integer, i As Integer
              Dim FileLength As Long, LeftOver As Long
              Dim FileData As String
              Dim RetVal As Variant
    
              On Error GoTo Err_ReadBLOB
    
              ' Open the source file.
              SourceFile = FreeFile
              Open Source For Binary Access Read As SourceFile
    
              ' Get the length of the file.
              FileLength = LOF(SourceFile)
              If FileLength = 0 Then
                  ReadBLOB = 0
                  Exit Function
              End If
    
              ' Calculate the number of blocks to read and leftover bytes.
              NumBlocks = FileLength \ BlockSize
              LeftOver = FileLength Mod BlockSize
    
              ' SysCmd is used to manipulate status bar meter.
              RetVal = SysCmd(acSysCmdInitMeter, "Reading BLOB", _
                       FileLength \ 1000)
    
              ' Put the record in edit mode.
              T.Edit
    
              ' Read the leftover data, writing it to the table.
              FileData = String$(LeftOver, 32)
              Get SourceFile, , FileData
              T(sField).AppendChunk (FileData)
    
              RetVal = SysCmd(acSysCmdUpdateMeter, LeftOver / 1000)
    
              ' Read the remaining blocks of data, writing them to the table.
              FileData = String$(BlockSize, 32)
              For i = 1 To NumBlocks
                  Get SourceFile, , FileData
                  T(sField).AppendChunk (FileData)
    
                  RetVal = SysCmd(acSysCmdUpdateMeter, BlockSize * i / 1000)
              Next i
    
              ' Update the record and terminate function.
              T.Update
              RetVal = SysCmd(acSysCmdRemoveMeter)
              Close SourceFile
              ReadBLOB = FileLength
              Exit Function
    
          Err_ReadBLOB:
              ReadBLOB = -Err
              Exit Function
    
          End Function
    
          '**************************************************************
          ' FUNCTION: WriteBLOB()
          '
          ' PURPOSE:
          '   Writes BLOB information stored in the specified table and field
          '   to the specified disk file.
          '
          ' PREREQUISITES:
          '   The specified table with the OLE object field containing the
          '   binary data must be opened in Visual Basic code (Access Basic
          '   code in Microsoft Access 2.0 or earlier) and the correct
          '   record navigated to prior to calling the WriteBLOB() function.
          '
          ' ARGUMENTS:
          '   T           - The table object containing the binary information.
          '   sField      - The OLE object field in table T containing the
          '                 binary information to write.
          '   Destination - The path and filename to write the binary
          '                 information to.
          '
          ' RETURN:
          '   The number of bytes written to the destination file.
          '**************************************************************
          Function WriteBLOB(T As Recordset, sField As String, _
          Destination As String)
              Dim NumBlocks As Integer, DestFile As Integer, i As Integer
              Dim FileLength As Long, LeftOver As Long
              Dim FileData As String
              Dim RetVal As Variant
    
              On Error GoTo Err_WriteBLOB
    
              ' Get the size of the field.
              FileLength = T(sField).FieldSize()
              If FileLength = 0 Then
                  WriteBLOB = 0
                  Exit Function
              End If
    
              ' Calculate number of blocks to write and leftover bytes.
              NumBlocks = FileLength \ BlockSize
              LeftOver = FileLength Mod BlockSize
    
              ' Remove any existing destination file.
              DestFile = FreeFile
              Open Destination For Output As DestFile
              Close DestFile
    
              ' Open the destination file.
              Open Destination For Binary As DestFile
    
              ' SysCmd is used to manipulate the status bar meter.
              RetVal = SysCmd(acSysCmdInitMeter, _
              "Writing BLOB", FileLength / 1000)
    
              ' Write the leftover data to the output file.
              FileData = T(sField).GetChunk(0, LeftOver)
              Put DestFile, , FileData
    
              ' Update the status bar meter.
              RetVal = SysCmd(acSysCmdUpdateMeter, LeftOver / 1000)
    
              ' Write the remaining blocks of data to the output file.
              For i = 1 To NumBlocks
                  ' Reads a chunk and writes it to output file.
                  FileData = T(sField).GetChunk((i - 1) * BlockSize _
                     + LeftOver, BlockSize)
                  Put DestFile, , FileData
    
                  RetVal = SysCmd(acSysCmdUpdateMeter, _
                  ((i - 1) * BlockSize + LeftOver) / 1000)
              Next i
    
              ' Terminates function
              RetVal = SysCmd(acSysCmdRemoveMeter)
              Close DestFile
              WriteBLOB = FileLength
              Exit Function
    
          Err_WriteBLOB:
              WriteBLOB = -Err
              Exit Function
    
          End Function
    
          '**************************************************************
          ' SUB: CopyFile
          '
          ' PURPOSE:
          '   Demonstrates how to use ReadBLOB() and WriteBLOB().
          '
          ' PREREQUISITES:
          '   A table called BLOB that contains an OLE Object field called
          '   Blob.
          '
          ' ARGUMENTS:
          '   Source - The path and filename of the information to copy.
          '   Destination - The path and filename of the file to write
          '                 the binary information to.
          '
          ' EXAMPLE:
          '   CopyFile "c:\windows\winfile.hlp", "c:\windows\winfil_1.hlp"
          '**************************************************************
          Sub CopyFile(Source As String, Destination As String)
              Dim BytesRead As Variant, BytesWritten As Variant
              Dim Msg As String
              Dim db As Database
              Dim T As Recordset
    
              ' Open the BLOB table.
              Set db = CurrentDb()
              Set T = db.OpenRecordset("BLOB", dbOpenTable)
    
              ' Create a new record and move to it.
              T.AddNew
              T.Update
              T.MoveLast
    
              BytesRead = ReadBLOB(Source, T, "Blob")
    
              Msg = "Finished reading """ & Source & """"
              Msg = Msg & Chr$(13) & ".. " & BytesRead & " bytes read."
              MsgBox Msg, 64, "Copy File"
    
              BytesWritten = WriteBLOB(T, "Blob", Destination)
    
              Msg = "Finished writing """ & Destination & """"
              Msg = Msg & Chr$(13) & ".. " & BytesWritten & " bytes written."
              MsgBox Msg, 64, "Copy File"
          End Sub
    						
  3. 建立下列新的資料表,然後將它儲存成 BLOB:
          Table: BLOB
          ------------------------
          Field Name: Blob
             Data Type: OLE Object
  4. 在 [設計] 檢視中開啟 BLOB 模組中,按一下 [偵錯視窗 (或在 Microsoft Access 2.0 或更早版本的即時運算視窗),在 [檢視] 功能表上。
  5. 在 [偵錯] 視窗中輸入下列命令並按下 ENTER:
          CopyFile "c:\windows\winfile.hlp", "c:\windows\winfil_1.hlp"
ReadBLOB()] 和 [WriteBLOB() 函數將 Microsoft Windows 說明] 檔案複製到 BLOB] 表格中,然後從該處呼叫 Winfil_1.hlp 磁碟檔案中的 [BLOB (二進位大型物件)] 欄位

這篇文章中的資訊適用於:
  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
關鍵字:?
kbmt kbinfo kbprogramming KB103257 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:103257? (http://support.microsoft.com/kb/103257/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。
Retired KB Article依現狀不再更新的知識庫內容免責聲明
本文旨在說明 Microsoft 不再提供支援的產品。因此,本文係依「現狀」提供,不會再更新。