'Use ADODB.Stream Method
'After ADO 2.6
'Import the Image to SQLServer
Private Sub ImportBLOB(cn As ADODB.Connection)
    
    Dim rs As New ADODB.Recordset
    Dim stm As ADODB.Stream
    
    Set stm = New ADODB.Stream
    
    ' Skip any table not found errors
    On Error Resume Next
    cn.Execute "drop table BinaryObject"
    
    On Error GoTo 0
    'Create the BinaryObject table
    cn.Execute "create table BinaryObject " & _
                 "(blob_id int IDENTITY(1,1), " & _
                  "blob_filename varchar(256), " & _
                  "blob_object image)"
                
    rs.Open "Select * from BinaryObject where 1=2", cn, adOpenKeyset, adLockOptimistic
    'Read the binary files from disk
    stm.Type = adTypeBinary
    stm.Open
    stm.LoadFromFile App.Path & "\BLOBsample.jpg"
    
    rs.AddNew
    rs!blob_filename = App.Path & "\BLOBsample.jpg"
    rs!blob_object = stm.Read
    
    'Insert the binary object in the table
    rs.Update
    
    rs.Close
    stm.Close
    
    Set rs = Nothing
    Set stm = Nothing
    
End Sub
'Display the image on image control
Private Sub DisplayBLOB(cn As ADODB.Connection)    Dim rs As New ADODB.Recordset
    
    ' Select the only image in the table
    rs.Open "Select * from BinaryObject where blob_id = 1", cn
    
    ' Set the DataSource to the recordset
    Set imgBinaryData.DataSource = rs
    'Set the DataField to the BLOB field
    imgBinaryData.DataField = rs!blob_object.Name
    
    'Release the recordset
    rs.Close
    Set rs = NothingEnd Sub

解决方案 »

  1.   

    如果将路径存放数据库中会比较简单
    qurstr = "select * from tikubiao where tu=" & "'" & u & "'"
    itemrs.Open (qurstr)
    'Set typers = problemdb.OpenRecordset(qurstr)
    If Not itemrs.EOF Then
    Picture1.Picture = LoadPicture("tu\" & itemrs("tu"))
    Else
    Picture1.Picture = LoadPicture("tu\empty.bmp")
    End If
      

  2.   

    存储PATH TO DATABASE ,IF THE PATH WAS CHANGED,THEN YOU DON‘T FOUND THE IMAGE。
      

  3.   

    LISHBIN(李)说的有道理,我开始也想在数据库中保存图片文件的路径。但后来就发觉这个问题。并且存在一个从数据库中获取路径并打开图片文件的时间问题。这样做并一不定是好事!
      

  4.   

    首先sqlserver存取图片数据字段为image
    存:
    Public Sub save_picture()
    Dim cnn As New ADODB.Connection, rst As New ADODB.Recordset
      Dim bit() As Byte
      dim varPath as string '图片的路径    cnn.open "连接数据库的字符串"
        sSql = "SELECT * FROM 表 WHERE 关键字='" & 关键值 & "'"
                                 '选出要增加或修改图片记录的记录
        rst.Open sSql, cnn, adOpenKeyset, adLockOptimistic
        
        If Not (rst.EOF And rst.BOF) Then    
           If VarPath = "" Then
            '    然后将字节数组的内容写入数据库即可
                rst.Fields("INFO_PICT") = ""
                rst.UPDATE
           Else
                Open VarPath For Binary As #1
                ReDim bit(LOF(1)) As Byte
                Get 1, 1, bit
                Close 1
             '    然后将字节数组的内容写入数据库即可
                rst.Fields("INFO_PICT").AppendChunk bit
                rst.UPDATE
           End If
        End If
    end sub 取:
    Public Sub show_picture()
    Dim REC As Recordset
    Dim sSql As String
    Dim I As Integer
    Set REC = New Recordset
    Dim bit1() As Byte
    Dim sa As String
    sSql = "SELECT * FROM 表 WHERE 关键字 ='" & 关键值 & "'"
       REC.Open sSql, Conn, adOpenStatic, adLockOptimistic, adCmdText
         If REC.EOF Or REC.BOF Then
            Exit Sub
         Else
            REC.Movefirst
            While Not REC.BOF
                Picture1.Picture = Nothing
                If REC("INFO_PICT").ActualSize > 0 Then
                    bit1 = REC.Fields("图片").GetChunk(REC("图片").ActualSize)
                    '然后将字节数组的内容拼装成文件即可
                    Open "c:\1.bmp" For Binary As #1
                  
                    Put 1, 1, bit1
                    Close 1
                    Picture1.Picture = LoadPicture"c:\1.bmp")  
                End If
                REC.Movenext
            Wend
         End If
        rec.close
        Set REC = Nothing
    Exit Sub
    Err:
       MsgBox "读取图片出错!", OKOnly, "系统提示"
    End Sub
      

  5.   

    Private Function Image2Chunk(Filename As String) As Variant
    On Error GoTo ProcErr
    Dim Datafile As Integer
    Dim FileLength As Long
    Dim chunk() As Byte
        
      Datafile = FreeFile
      Open Filename For Binary Access Read As Datafile
        FileLength = LOF(Datafile)
        If FileLength = 0 Then GoTo ProcErr
        ReDim chunk(FileLength)
        Get Datafile, , chunk()
      Close Datafile
        
    ProcExit:
      Image2Chunk = chunk()
      Exit FunctionProcErr:
      Image2Chunk = 0
    End FunctionDim chunk() As Byte
            chunk() = Image2Chunk(tmpFile1)
            .Fields("tp1").AppendChunk chunk()
            .Update
      

  6.   

    http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=19363