用SQL语句录入的话应该是FIELD(1)。APPENDCHUNK

解决方案 »

  1.   

    用ADO存放文件到数据库Binary字段
    存文件到数据库Public Function AddFile() As Boolean    'Return boolean to decide whether refresh files list    Dim strBin As String * 3000    Dim btyGet() As Byte    Dim lngBlockIndex As Long    Dim lngBlocks As Long    Dim lngLastBlock As Long    Dim lngPosition As Long    Dim lngFileLenth As Long    Dim lngIndex As Long        With frmBinary.CommonDialog1        '.InitDir = App.Path        .Filter = "All image files|*.bmp;*.ico;*.jpg;*.gif|Bitmap files|*.bmp|Icon files|*.ico|All files|*.*"        .FileName = ""        On Error GoTo ErrorHandle        .ShowOpen        On Error GoTo 0        If .FileName <> "" Then            Open .FileName For Binary As #1            lngFileLenth = LOF(1)                        lngPosition = 0                        'Get block count for loop            lngBlocks = lngFileLenth \ BlockSize                        'Get lngth of last block for the last read            lngLastBlock = lngFileLenth Mod BlockSize                        rsBinary.AddNew            rsBinary.Fields("typecode") = TypeCode                        For lngBlockIndex = 1 To lngBlocks                ReDim btyGet(BlockSize)                Get #1, , btyGet()                rsBinary.Fields("content").AppendChunk btyGet()                lngPosition = lngPosition + BlockSize            Next            If lngLastBlock > 0 Then                ReDim btyGet(lngLastBlock)                Get #1, , btyGet()                rsBinary.Fields("content").AppendChunk btyGet()            End If                        rsBinary.Update            Close #1                        AddFile = True            MsgBox "Save finished", vbInformation        Else            AddFile = False        End If    End With    Exit FunctionErrorHandle:    AddFile = FalseEnd Function从数据库取文件出来Public Sub SaveFile(ByVal FileID As Long)    Dim lngBlockCount As Long    Dim lngLastBlock As Long    Dim lngI As Long    Dim btyBlock() As Byte    Dim lngResult As Long        If rsBinary.EOF And rsBinary.BOF Then Exit Sub    rsBinary.MoveFirst    rsBinary.Find " id=" & FileID    If Not rsBinary.EOF Then        With frmBinary.CommonDialog1            .FileName ="TempSave"            '.InitDir = App.Path                        'If user cancel save the goto handle            On Error GoTo ErrorHandle            .ShowSave            If .FileName <> "" Then                lngBlockCount = rsBinary.Fields("content").ActualSize \ BlockSize                lngLastBlock = rsBinary.Fields("content").ActualSize Mod BlockSize                                If Dir(.FileName) <> "" Then                    If MsgBox("File " & .FileName & " is exist,overwrite?", vbYesNo + vbQuestion) = vbYes Then                        Kill .FileName                    Else                        Exit Sub                    End If                Else                End If                                    Open .FileName For Binary As #1                                ReDim btyBinary(BlockSize)                                For lngI = 1 To lngBlockCount                    btyBlock() = rsBinary.Fields("content").GetChunk(BlockSize)                    Put #1, , btyBlock                Next                                If lngLastBlock <> 0 Then                    ReDim btyBlock(lngLastBlock)                    btyBlock() = rsBinary.Fields("content").GetChunk(lngLastBlock)                    Put #1, , btyBlock                End If                                Close #1                MsgBox .FileName & " is saved", vbInformation            Else            End If        End With    End If        Exit SubErrorHandle:    End Sub
      

  2.   

    'Save photo to database
    Private Sub SaveToDB(ByRef fld As ADODB.Field, DiskFile As String)
        Dim bytedata() As Byte
        Dim NumBlocks As Long
        Dim FileLength As Long
        Dim LeftOver As Long
        Dim SourceFile As Long
        Dim i As Long
        Const BLOCKSIZE = 4096    SourceFile = FreeFile
        Open DiskFile For Binary Access Read As SourceFile
        FileLength = LOF(SourceFile)
        If FileLength = 0 Then
           Close SourceFile
           MsgBox DiskFile & "错误的图片内容!"
        Else
            NumBlocks = FileLength \ BLOCKSIZE
            LeftOver = FileLength Mod BLOCKSIZE
            fld.Value = Null
            ReDim bytedata(BLOCKSIZE)
            For i = 1 To NumBlocks
                Get SourceFile, , bytedata()
                fld.AppendChunk bytedata()
            Next i
            ReDim bytedata(LeftOver)
            Get SourceFile, , bytedata()
            fld.AppendChunk bytedata()
            Close SourceFile
        End If
    End Sub  ‖天天写程序‖  
      ‖夜夜泡小妞‖
    ‖身兼数职做代码‖
    ‖晚晚工作到天明‖
    ‖为何人生如此苦‖
    ‖泡妞消费数目高‖
    ‖我看世俗本无趣‖
    ‖程序伤神妞伤人‖
      ‖不再见女人‖  
      ‖不想写程序‖