'保存
Private Sub cmdSave_Click()
    
    Dim rs As New ADODB.Recordset
    Dim InterID As String
    Dim strData As String
    Dim fileNo As Integer
    Dim LenF As Long
    Dim BMPData() As Byte
    Dim mFileName As String
    '先保存图片
    mFileName = "c:\tt.bmp"
    SavePicture Picture1.Image, mFileName
    fileNo = FreeFile()
    '打开文件取出图形数据
    Open mFileName For Binary As #fileNo
    LenF = LOF(fileNo)
    ReDim BMPData(LenF)
    Get #fileNo, , BMPData
    Close #fileNo
    '保存到数据库中
    With rs
        .ActiveConnection = sConnection
        .LockType = adLockOptimistic
        .Open "select * from test "
        .AddNew
        '字段FID为一个 int 型的主键
        '字段FData为一个image 型的字段
        .Fields("FID") = InterID
        .Fields("FData").AppendChunk BMPData
        .Update
    End With
    rs.Close
    Set rs = Nothing
    
    
End Sub
'得到
Private Sub cmdLoad_Click()
    On Error GoTo H_Error
    Dim rsData As New ADODB.Recordset
    Dim strData As String, strBuffer As String
    Dim LenF As Long
    Dim BMPData() As Byte
    '打开数据库
    With rsData
        .ActiveConnection = sConnection
        .CursorLocation = adUseClient
        .Open "select * from test"
        '得到数据的长度
        LenF = .Fields("FData").ActualSize
        ReDim BMPData(LenF)
        '得到图形文件的数据
        BMPData = .Fields("FData").GetChunk(LenF)
    End With
    rsData.Close
    Set rsData = Nothing
    strBuffer = "c:\test.bmp"
    If Dir(strBuffer) <> "" Then
        Kill strBuffer
    End If
    Dim fileNo As Integer
    fileNo = FreeFile()
    Open strBuffer For Binary As #fileNo
    Put #fileNo, , BMPData
    Close #fileNo
    Picture1.Picture = LoadPicture(strBuffer)
    Exit Sub
H_Error:
    Debug.Assert False
End Sub

解决方案 »

  1.   

    问题:
    1。图片保存到SQL SERVER,是否很占空间。
    2。存取时的速度如何。请vcchen调试成功后,告知。[email protected]
      

  2.   

    是否可以同样方法存取*.gif,或*.jpg
      

  3.   

    同样方法可以存取*.gif,或*.jpg
      

  4.   

    DeD(似水年华):
    "没有压缩当然是有多大就占用多少空间"中的“压缩”指什么
    如何实现
      

  5.   

    最简单的办法是
    在保存之前先调用一个控件对文件进行压缩
    然后保存压缩后的文件
    在打开时进行取数-->保存--->解压缩还原
    我推荐ActiveZip Control Version 2.0挺好支持Winzip
      

  6.   

    由于我的机子及网络环境还不错,都是P2和P3的机子,一般我认为在局域网中的问题不是很严重,但还是有延迟,我的机子没有zip控件,采取折中方案,所有的图片都采用jpg文件,直接送进数据库后,取出后恢复再调用,速度可以保证,但大量的图片在库中时没有试验。