'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.   

    我们可是用的vb6.0 ado2.0 怎么办?
      

  2.   

    '写到数据文件中
    Private SubWriteImageFile(ByVal strFilename As String)    
        Dim intFileNum As Integer
        Dim m_lngPostion  As Long    strFilename = App.Path & "\jpeg\" & strFilename
        intFileNum = FreeFile
        Open strFilename For Binary As #intFileNum
        m_lngPostion = FileLen(strFilename) + 1
        Put #intFileNum, m_lngPostion, bytData
        Close #intFileNum
    End Function
      

  3.   

    '写到数据文件中 
    Private Sub WriteImageFile(ByVal strFilename As String,rd as ADODB.Recordset) 
        Dim intFileNum As Integer
        Dim m_lngPostion  As Long
        dim bytData() As Byte
        bytData=rd("ImageField")
        intFileNum = FreeFile
        Open strFilename For Binary As #intFileNum
        m_lngPostion = FileLen(strFilename) + 1
        Put #intFileNum, m_lngPostion, bytData
        Close #intFileNum
    End Sub
      

  4.   

    老兄,我们之间有点误解,image不是图象文件,而是一个数据流,
    而且你的代码能不能解释一下
    万分感谢!!!
      

  5.   

    Private Sub showPhoto()
        
    dim rec=new oledb.recorderset    Dim sSQL As String
        
        sSQL = "Select image字段 From 表名 Where 条件"
        Set rec = dataMain.OpenRecordset(sSQL, dbOpenSnapshot)
        If rec.EOF And rec.BOF Then
            Exit Sub
        End If
        DisplayImageFromSybase imgPhoto, recPhoto.Fields(0)
       
        Set rec = Nothing
    End SubPublic Sub DisplayImageFromSybase(fn As string ,fField As Field)
     
        Dim DataFile As Integer, FileName As String
        Dim Chunks As Integer, Fragment As Integer, lLen As Long
        Dim Chunk() As Byte
        Dim i As Integer
        
        Const ChunkSize As Integer = 30000
           FileName = fn    
        lLen = fField.FieldSize
        
        If lLen = 0 Then
            Exit Sub
        End If
            
        Open FileName For Binary Access Write As #1
        
        Chunks = lLen \ ChunkSize
        Fragment = lLen Mod ChunkSize
     
        ReDim Chunk(Fragment)
        Chunk() = fField.GetChunk(0, Fragment)
        Put #1, Chunk()
     
        For i = 1 To Chunks
            ReDim Buffer(ChunkSize)
            Chunk() = fField.GetChunk(Fragment + (i - 1) * ChunkSize,ChunkSize)
            Put #1, , Chunk()
        Next i
        
        Close #1
        End Sub
      

  6.   

    DisplayImageFromSybase imgPhoto, recPhoto.Fields(0)
    应为:
    DisplayImageFromSybase imgPhoto, rec.Fields(0)
      

  7.   

    老兄.我的image不是图片,是数据流
    你怎么变成显示图片拉
    继续要求支援
      

  8.   


    新建一个工程,添加 ado 控件,2个 Command ,1个 Picture,1个 ImageDim Chunk() As Byte
    Dim lngLengh As Long
    Dim intChunks As Integer
    Dim intFragment As Integer
    Const ChunkSize = 1000
    Const lngDataFile = 1Private Sub cmdBrowse_Click()
        
        On Error Resume Next
        With cmdlFilePath
            .Filter = "JPG Files|*.JPG|Bitmaps|*.BMP"
            .ShowOpen
            txtFilePath.Text = .filename
        End With
    End SubPrivate Sub Savepic()
        
        Open "c:\colordraw0094_m.jpg" For Binary Access Read As lngDataFile
        lngLengh = LOF(lngDataFile)
        If lngLengh = 0 Then Close lngDatafile: Exit Sub
        intChunks = lngLengh \ ChunkSize
        intFragment = lngLengh Mod ChunkSize
        
        'OpenData 打开数据库
        Dim i As Integer
        Dim rs As New ADODB.Recordset
        Dim strQ  As String
        
        If rs.State = adStateOpen Then rs.Close
        
        strQ = "Select * From [mydata]"
        rs.Open strQ, conn, adOpenStatic, adLockOptimistic
        
        On Error Resume Next
        
        rs.AddNew
        
        ReDim Chunk(intFragment)
        Get lngDataFile, , Chunk()
        rs.Fields("rs_photo1").AppendChunk Chunk()
        ReDim Chunk(ChunkSize)
        
        For i = 1 To intChunks
            Get lngDataFile, , Chunk()
            rs.Fields("rs_photo1").AppendChunk Chunk()
        Next i
        
        rs.Update
        rs.Close
        Close lngDataFile
        Call ShowPic
        
    End SubPublic Sub ShowPic()    'OpenData 打开数据库
        Dim i As Integer
        Dim rs As New ADODB.Recordset
        Dim strQ, filename As String
        If rs.State = adStateOpen Then rs.Close    strQ = "Select * From [mydata]"
        rs.Open strQ, conn, adOpenStatic, adLockOptimistic
        If rs.EOF <> True Then
            rs.MoveLast
        Else
            Exit Sub
        End If
        On Error Resume Next
        Open "pictemp" For Binary Access Write As lngDataFile
            lngLengh = rs.Fields("rs_photo1").ActualSize
            intChunks = lngLengh \ ChunkSize
            intFragment = lngLengh Mod ChunkSize
            ReDim Chunk(intFragment)
            Chunk() = rs.Fields("rs_photo1").GetChunk(intFragment)
            Put lngDataFile, , Chunk()
            For i = 1 To intChunks
               ReDim Buffer(ChunkSize)
               Chunk() = rs.Fields("rs_photo1").GetChunk(ChunkSize)
               Put lngDataFile, , Chunk()
            Next i
        Close lngDataFile
        filename = "pictemp"
        Picture1.Picture = LoadPicture(filename)
        Image1.Stretch = True
        Image1.Picture = Picture1.Picture
        Kill filename
        
    End SubPrivate Sub Command1_Click()
        
        SavepicEnd SubPrivate Sub Command2_Click()    ShowPic
        
    End Sub
      

  9.   

    上面写的是acess的代码!楼主可以改一下连接数据库的设置代码用于sql server!
      

  10.   

    各位,我说的很清楚拉,image不是图片而是数据流