读进Sql Server中的图片是二进制流,如何把它还原出来?

解决方案 »

  1.   

    '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  =  Nothing  
     
    End  Sub  
      

  2.   

    微软的答案:http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q258/0/38.asp&NoWebContent=1
      

  3.   

    '将任何文件从数据库中下载到本地: 
        Public Function LoadFile(ByVal col As ADODB.Field, ByVal FileName As String) As Boolean '获得binary数据 
        On Error GoTo myerr: 
         Dim arrBytes() As Byte 
         Dim FreeFileNumber As Integer 
         lngsize = col.ActualSize 
         arrBytes = col.GetChunk(lngsize) 
         FreeFileNumber = FreeFile 
         Open FileName For Binary Access Write As #FreeFileNumber 
         Put #FreeFileNumber, , arrBytes 
         Close #FreeFileNumber 
         LoadFile = True 
        myerr: 
         If Err.Number <> 0 Then 
         LoadFile = False 
         Err.Clear 
         End If 
        End Function 
         
        '将文件从本地上传到数据库中 
        Public Function UpLoadFile(ByVal FileName, ByVal col As ADODB.Field) As Boolean 
         On Error GoTo myerr: 
         Dim arrBytes() As Byte 
         Dim FreeFileNumber As Integer 
         FreeFileNumber = FreeFile 
         Open FileName For Binary As #FreeFileNumber 
         n = LOF(FreeFileNumber) 
         ReDim arrBytes(1 To n) As Byte 
         Get #FreeFileNumber, , arrBytes 
         Close #FreeFileNumber 
         col.AppendChunk (arrBytes) 
         UpLoadFile = True 
        myerr: 
         If Err.Number <> 0 Then 
         UpLoadFile = False 
         Err.Clear 
         End If 
        End Function
      

  4.   

    to  alicky(周松) 我们刚用vb不久,你能不能给一个实际的例子?
    看了你的函数不知道怎么调用?thanks!
      

  5.   

    那如果要用Command对象Insert该怎么写呢?
    cmd.CommandText="insert into tbl_Image values(" & mStream.Read & ")"
    cmd.Execute
    不知道为什么这样要报错
      

  6.   

    现在还有个问题,如果用数组来作为中介,将图片(jpeg)放到数据库中后,由于image 类型是二进制的,那么图片在数据库中的大小将是原大小的几倍,有什么办法可以解决这个问题吗?