ACC: Čítanie, ukladanie & písanie binárne veľké objekty (objektov BLOB)

ID článku: 103257 - Zobraziť produkty, ktorých sa tento článok týka.
Rozšírené: Vyžaduje expertné kódovanie, interoperability a viacerými používateľmi zručnosti.

Rozbaliť všetko | Zbaliť všetko

SUHRN

Môžete ukladať veľké dátové objekty (ako napríklad zvuku, videa alebo grafických údajov) v pole s typom údajov OLE Object v tabuľke programu Microsoft Access. Niektoré veľké binárne údaje objekty nemôžu byť však predstavoval ak tak neurobia mať server OLE, ktorý chápe údaje sú uložené. Môžete tiež ukladať kópie spustiteľný program súbory alebo iné údaje OLE Tabuľka programu Microsoft Access. Tento typ dát sa označuje ako binárny veľké objekt bitová mapa (BLOB).

Tento článok predpokladá, že ste oboznámení s programom Visual Basic pre Aplikácie a s vytváraním aplikácie Microsoft Access pomocou programovacie nástroje s Microsoft Access. Ďalšie informácie o Visual Basic for Applications, pozrite sa na vašu verziu "Budovanie aplikácií s Microsoft Access" manuál.

Poznámka: Visual Basic for Applications sa nazýva základný prístup v programe Microsoft Prístup verzia 1.x a 2.0. Ďalšie informácie o základných. prístup, nájdete "Úvod do programovanie" príručke v programe Microsoft Prístup verzia 1.x alebo "Stavebné aplikácie" manuál v programe Microsoft Access verzie 2.0.

DALSIE INFORMACIE

Nasledujúci príklad obsahuje dve vzorky používateľom definované funkcie, ktoré ste môžete použiť na spravovanie veľkého rozsahu binárne údaje v poli s OLE Typ údajov objekt. Používateľom definované funkcie sú ReadBLOB() a WriteBLOB().

  • ReadBLOB() funkcia číta a binárne disku súbor a sklady je v poli Typ OLE Object.
  • Funkcia WriteBLOB() píše binárne údaje uložené v OLE Object pole do súboru na disku.
Tento príklad demonštruje ako binárny súbor skopírovať do OLE Object pole, a potom ako napísať ho späť na nový disk súbor:

  1. Vytvoriť nový modul nazvaný BLOB a zadajte nasledovné riadky v module vyhlásenia sekcia:
    Možnosť explicitné
    Const BlockSize = 32768
    Poznámka: Ak pracujete s Microsoft Access 2.0, budete musieť po predchádzajúcich dvoch zahŕňať nasledujúce definície:
          Const dbOpenTable = DB_OPEN_TABLE
          Const acSysCmdInitMeter = SYSCMD_INITMETER
          Const acSysCmdUpdateMeter = SYSCMD_UPDATEMETER
          Const acSysCmdRemoveMeter = SYSCMD_REMOVEMETER
  2. Zadajte nasledujúci kód v module.

    Poznámka: V nasledujúcom kóde vzorky podčiarkovník (_) na konci vedenie používa ako znak pokračovania riadku. Odstrániť znak podčiarknutia od konca riadku, keď vykonß tento kód v prístup základné.

    Poznámka: Tieto techniky nebude pracovať pre Microsoft Access 1.x. Pre Microsoft Access 1.x, budete musieť upraviť kód tak že používa tabuľku premenné namiesto premenných záznamov a OpenTable Funkcia namiesto 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. Vytvorte nasledujúcou novou tabuľkou a potom ho uložiť ako objekt BLOB:
          Table: BLOB
          ------------------------
          Field Name: Blob
             Data Type: OLE Object
  4. S modulom BLOB otvorenú v návrhovom zobrazení, kliknite na okno Debug (alebo Okamžitom okne Microsoft Access 2.0 alebo staršia) na Ponuka Zobraziť.
  5. Zadajte nasledujúci riadok v okne ladenia, a potom stlačte kláves ENTER:
          CopyFile "c:\windows\winfile.hlp", "c:\windows\winfil_1.hlp"
Funkcie ReadBLOB() a WriteBLOB() kopírovať Pomocníka systému Microsoft Windows súbor na pole Blob v tabuľke BLOB, a potom tam na disk súbor s názvom Winfil_1.hlp

Vlastnosti

ID článku: 103257 - Posledná kontrola: 10. októbra 2011 - Revízia: 2.0
Informácie v tomto článku sa týkajú nasledujúcich produktov:
  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 97 Standard Edition
Kľúčové slová: 
kbinfo kbprogramming kbmt KB103257 KbMtsk
Strojovo preložené
DÔLEŽITÉ: Tento článok bol preložený pomocou softvéru na strojový preklad od spoločnosti Microsoft, nie prekladateľom. Spoločnosť Microsoft ponúka články preložené prekladateľmi aj strojovo preložené články, vďaka čomu máte možnosť prístupu ku všetkým článkom databázy Knowledge Base vo svojom jazyku. Strojovo preložený článok však nie je vždy perfektný. Môže obsahovať chyby týkajúce sa slovnej zásoby, syntaxe alebo gramatiky, podobne ako cudzinec môže robiť chyby, keď rozpráva vašim jazykom. Spoločnosť Microsoft nenesie zodpovednosť za akékoľvek nepresnosti, chyby alebo škody spôsobené akýmkoľvek nepresným prekladom obsahu alebo jeho použitím zo strany zákazníkov. Spoločnosť Microsoft softvér na strojový preklad pravidelne aktualizuje.
Pokiaľ chcete vidieť anglickú verziu článku, kliknite sem:103257
Upozornenie na neaktuálny obsah článku databázy KB
Tento článok obsahuje informácie o produktoch, pre ktoré spoločnosť Microsoft už neposkytuje technickú podporu. Z tohto dôvodu je tento článok publikovaný ako nezmenený a už nebude aktualizovaný.

Odošlite odozvu